Last active
December 14, 2015 18:59
-
-
Save caevyn/5133306 to your computer and use it in GitHub Desktop.
Exclude sensitive form data from elmah logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in global.asax | |
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e) | |
{ | |
var ctx = e.Context as HttpContext; | |
if(ctx == null) | |
{ | |
return; | |
} | |
ElmahSensitiveDataFilter.Apply(e, ctx); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ElmahSensitiveDataFilter | |
{ | |
public static void Apply(ExceptionFilterEventArgs e, HttpContext ctx) | |
{ | |
var sensitiveFormData = ctx.Request.Form.AllKeys | |
.Where(key => key.Equals("password", StringComparison.OrdinalIgnoreCase)).ToList(); | |
if (sensitiveFormData.Count == 0) | |
{ | |
return; | |
} | |
var error = new Error(e.Exception, ctx); | |
sensitiveFormData.ForEach(k => error.Form.Set(k, "*****")); | |
Elmah.ErrorLog.GetDefault(null).Log(error); | |
e.Dismiss(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment