[SOLVED] Will the static public variables in my app get shared with other users in the same app?


For reasons I would rather not discuss, I need to create a custom authentication system for my app. I was just reviewing the system and am having some doubts if my solution is thread safe. My goal was to create a solution that would allow my app to authenticate a user one time and that users authentication info would be shared by all master pages, pages, classes, user controls, etc that are used. (But not share the same info between users)

Here is my setup:

PageHttpModule.cs – this is added to the web.config as a httpModule.

public class PageHttpModule : IHttpModule
    public void Init(HttpApplication app)
        app.AuthenticateRequest += new EventHandler(OnAuthenticateRequest);

    public void OnAuthenticateRequest(Object s, EventArgs e)

    public void Dispose() { }


public static class CurrentUser
  public static bool IsAuthenticated { get; private set; }
  public static string Email {get; set;}
  public static string RealName {get; set;
  public static string UserId {get; set;}

    public static void Initialize()

    Note: this is a scaled down version of my authentication code.

    public static void AuthenticateUser()
        UserAuthentication user = new UserAuthentication();

        if (user.IsAuthenticated)
            CurrentUser.IsAuthenticated = true;
            CurrentUser.UserId = user.UserId;
            CurrentUser.Email = user.Email;
            CurrentUser.RealName = user.RealName;


public class UserAuthentication

    public string Email { get; set; }
    public string RealName { get; set; }
    public string UserId { get; set; }
    public bool IsAuthenticated { get; private set; }

    public UserAuthentication()
        IsAuthenticated = false;
        Email = String.Empty;
        RealName = String.Empty;
        UserId = String.Empty;

    public void AuthenticateUser()

        //do some logic here.. if the user is ok then 
        IsAuthenticated = true
        Email = address from db
        UserId = userid from db;
        Realname = name from db;

I have tested between 3 different browsers and it seems to work fine, but I am still learning and don’t want to make a huge mistake.

If my logic is totally wrong, then how should I do it so I dont have to put user lookups on every page directly?


No, this is not thread-safe. For instances of the application living in separate processes or AppDomains, this will be just fine. But if your ASP.NET server is going to serve multiple requests at once using threading, you are going to have some very bad side effects if two people try to use the application at the same time.

Answered By – cdhowie

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *