This project is read-only.

A try at a UsernamePropertyReader

Jun 3, 2008 at 11:27 PM
I have been playing around with this. Great stuff - thanks!

I would like to be able to log the username of the current user accessing my asp.net website. I have tried writing a UsernamePropertyReader, it seems to work.. sort of. The problem is, that the HttpContext.User is null when the Application_BeginRequest event is raised. It might be possible to retrieve the username another way. Anyway, here is my code:

    /// <summary>
    /// A <see cref="PropertyReader"/> that gets the name of the current user using the <see cref="HttpContext.Current.User.Identity.Name"/> property.
    /// </summary>
    public class UsernamePropertyReader : StringPropertyReader
    {
        public override bool TryGetValue(out object value, TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] data)
        {
            value = null;
            // HttpContext might be null if application is shutting down.
            // User property is null during none web requests, and before a certain point in the page life cycle.
            if (HttpContext.Current != null && HttpContext.Current.User != null)
            {
                value = HttpContext.Current.User.Identity.Name;
                return true;
            }
            return false;
        }
    }

Any ideas on how to improve this?

Regards, Egil.
Jun 5, 2008 at 11:14 AM
Hi Egil,

I've been talking to Josh about this and there are a number of property readers that he wants to implement (the ASP.Net user name being one of them)!

The HttpContext.User isn't set until authentication has happened. After the AuthenticateRequest event, HttpContext.User should be non-null, and ASP.Net uses it to set Thread.CurrentPrincipal (so we can't use that in this case either!). For more details see the section "The anatomy of a WebRequest" in http://msdn.microsoft.com/en-us/library/aa302376.aspx. The AuthenticateRequest event happens quite early in the page lifecycle, but BeginRequest is the very first event (see http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx) so we can't do anything generic in BeginRequest. If you need logging to include the user details in BeginRequest then you'd need to create a PropertyReader specific to the authentication module you're using. For example, if you're using Windows authentication then you could read the LOGON_USER server variable from the request. For forms authentication, you'd need to get the details from the authentication cookie.

Hope that helps!

Stuart
Jun 5, 2008 at 1:08 PM
Thanks for the input Stuart.

I am looking forward to see what property readers you guys come up with. I actually don't mind not being able to read the user name during BeginRequest. The user name is only really interesting during business logic, which do not happen at this stage anyway. It would of course be a added bonus to have the user name information all the time.

Regards, Egil.