Hi, 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
+5
A:
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 Harvey
2010-04-12 17:29:47
One of the benefits of ASP.NET MVC is that it eliminates the need to encode large amounts of Session data in the web page.
Robert Harvey
2010-04-12 17:38:04
@Robert - 'encode large amounts of Session data in the web page'. Unsure what you mean here. I was referring to the Session in memory in IIS. Sounds like you're referring to webforms ViewState? I'm confused by your comment. :)
p.campbell
2010-04-12 17:41:32
Mmm, think you're right. Got my terminology confused. That said, I have yet to use Session in an ASP.NET MVC app, given the ability to use ViewModel classes, ViewData and data binding to do the same thing.
Robert Harvey
2010-04-12 17:44:32
Not my downvote, by the way. :)
Robert Harvey
2010-04-12 17:47:17
+1
A:
Robert's answer is correct. Another alternative is to use the Thread
class:
System.Threading.Thread.CurrentPrincipal.Identity.Name
Max Toro
2010-04-12 17:34:41