Based on my current understanding I do not think this code is thread safe but want to confirm. In other words I think that, although it would be extremely unlikely, multiple threads representing different HTTP requests could potentially mix up the value of the _userName
property.
public class SomeClass
{
private static string _userName;
public static string UserName
{
get
{
if (string.IsNullOrEmpty(_userName))
{
_userName = HttpContext.Current.User.Identity.Name;
}
return _userName;
}
}
}
Is it thread-safe and if not would removing the null check, and always accessing HttpContext.Current.User.Identity.Name
directly (in a static property) be thread-safe?
public class SomeClass
{
public static string UserName
{
get
{
return HttpContext.Current.User.Identity.Name;
}
}
}