Tony
Tony

Reputation: 12695

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 namespace 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

Upvotes: 2

Views: 910

Answers (3)

Max Toro
Max Toro

Reputation: 28608

Robert's answer is correct. Another alternative is to use the Thread class:

System.Threading.Thread.CurrentPrincipal.Identity.Name

Upvotes: 1

p.campbell
p.campbell

Reputation: 100567

Have you considered using the Session to store user-related data?

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180788

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

Upvotes: 5

Related Questions