ASP.NET MVC - How to save some data between requests?
I'm trying to solve the problem: when a user is being logged into a WebSite (via user control .ascx stored on the Master Page), it's name is being stored in a Page.User.Identity.Name property. OK, but how to retrieve that user name in the controller ? Is it possible without registering System.Security.Principal namesp开发者_JAVA百科ace in the controller ? In the other words - the controller must know whose user wants to do some action (e.g. change account data). I could store it's name in the Html.Hidden control on each View but I don't want to have a mess in my Views
IPrincipal User
is one of the members in your controller (it is a property), so all you have to do to get the name of the currently logged in user in your controller method is
string userName = User.Identity.Name
Robert's answer is correct. Another alternative is to use the Thread
class:
System.Threading.Thread.CurrentPrincipal.Identity.Name
Have you considered using the Session to store user-related data?
精彩评论