Search code examples
c#asp.net-mvcadfselmah

Losing USER information in ELMAH when using ADFS


I have an MVC web application using ELMAH to log unhandled exception. I'm now moving from windows authentication to ADFS authentication. The application has been adapted and is working fine. However now when I check the error, the user information is not there anymore. Which makes sense as ELMAH is using the context identity and not the claims to retrieve this info. Does anyone of you have an idea how I could do to get this information logged again?


Solution

  • You can enrich ELMAH errors using error filtering hook if you can accept a small hack. In short, you need to implement the ErrorLog_Filtering method in the Global.asax.cs file:

    void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
    {
        var httpContext = args.Context as HttpContext;
        if (httpContext != null)
        {
            var error = new Error(args.Exception, httpContext);
            error.User = GetUserFromDatabase();
            ErrorLog.GetDefault(httpContext).Log(error);
            args.Dismiss();
        }
    }
    

    In the example, I update the User property with the value of a dummy method. How you want to implement this depends on how you would get the currently logged in user from ADFS. Finally, I log the error again and dismiss the initial (user-less) error.