Reputation: 669
I need to store the current user name for every save request/user in SQL table. It is MVC 4 based application and hosted in IIS server. Also, it is an internal tool and NTLM based authentication.
I have got the username of current user using HttpContext.Current.User.Identity.Name
My question is how the global variable reacts in MVC? What would happen for "currentUser" variable when multiple requests comes? Will the currentUser creates new value for every requests? Please help me to understand.
Sample Code:
public class ClearCacheController : ApiController
{
private string currentUser = HttpContext.Current.User.Identity.Name.ToLower();
public void method1()
{
SaveValue1(currentUser);
}
public void method2()
{
SaveValue2(currentUser);
}
public void method3()
{
SaveValue3(currentUser);
}
}
Upvotes: 0
Views: 198
Reputation: 239270
Controllers are instantiated and disposed for each unique request. So, something like User.Identity.Name
is not really a global variable. It's an instance variable on the controller. (Well, User
is an instance variable on the controller, while Identity
is an instance variable on User
, etc.). Long and short, it will only hold the value of the user who made the particular request being executed, not any user making any request.
Upvotes: 2