user203687
user203687

Reputation: 7237

Is it thread-safe to write this?

Can I write something like the following (in an assembly being used in an ASP.NET web page)?

public static string CurrentAuthenticatedUserFromHttpRequest
{
    get
    {
        if (HttpContext.Current.Items["AuthUser"] == null)
        {
            return string.Empty;
        }

        return HttpContext.Current.Items["AuthUser"].ToString(); //set in "TryAuthenticate"
    }
}

It is going to be a static read-only property. The value (to HttpContext.Current.Items["AuthUser"]) is set through a httphandler.

Just wondering on how this would perform during multiple requests. Is the data going to be accurate when multiple users try to access the same property (in multiple requests), even when high volumes of requests come in?

Upvotes: 3

Views: 131

Answers (1)

flup
flup

Reputation: 27104

Yes, this is threadsafe. The static HttpContext.Current property differs per thread and contains the context for the request that the thread is currently handling.

Upvotes: 5

Related Questions