ASP.NET MVC - How to preserve some data between requests?

I am trying to solve a problem: when a user logs into the WebSite (via a .ascx custom control stored in the master page) that name is stored in the Page.User.Identity.Name property. Ok, but how do I get this username in the controller? Is this possible without registering the System.Security.Principal namespace in the controller? In other words, the controller needs to know whose user wants to take some action (for example, change account details). I could store its name in Html.Hidden control on every view, but I don't want to have a mess in my views

+2


source to share


3 answers


IPrincipal User

is one of the members of your controller (this is a property), so all you need to do to get the name of the current user in your controller method is -



string userName = User.Identity.Name

      

+5


source


Robert answered correctly. Another alternative is to use the class Thread

:



System.Threading.Thread.CurrentPrincipal.Identity.Name

      

+1


source


Have you considered using a session to store user-related data?

0


source







All Articles