How to get the current user in ASP.NET MVC
Solution 1
If you need to get the user from within the controller, use the User
property of Controller. If you need it from the view, I would populate what you specifically need in the ViewData
, or you could just call User as I think it's a property of ViewPage
.
Solution 2
I found that User
works, that is, User.Identity.Name
or User.IsInRole("Administrator")
.
Solution 3
Try HttpContext.Current.User
.
Public Shared Property Current() As System.Web.HttpContext
Member of System.Web.HttpContextSummary:
Gets or sets the System.Web.HttpContext object for the current HTTP request.Return Values:
The System.Web.HttpContext for the current HTTP request
Solution 4
You can get the name of the user in ASP.NET MVC4 like this:
System.Web.HttpContext.Current.User.Identity.Name
Solution 5
I realize this is really old, but I'm just getting started with ASP.NET MVC, so I thought I'd stick my two cents in:
Request.IsAuthenticated
tells you if the user is authenticated.Page.User.Identity
gives you the identity of the logged-in user.
Related videos on Youtube
Serhat Ozgel
Software developer and consultant. Areas of expertise: Web application and api development, software development, cloud architecture, mobile application backend development, e-commerce software development and integration.
Updated on July 08, 2022Comments
-
Serhat Ozgel almost 2 years
In a forms model, I used to get the current logged-in user by:
Page.CurrentUser
How do I get the current user inside a controller class in ASP.NET MVC?
-
Serhat Ozgel over 15 yearsWhere does this Membership class come from? IntelliSense does not recognize it by default.
-
Serhat Ozgel over 15 yearsApparently HttpContext does not have a property named "Current".
-
Sean over 15 yearsThe System.Web.Security namespace. I'm not positive this is useful in MVC but I use it with the login controls for Web Forms.
-
Jeff Sheldon over 15 yearsI believe you two are talking about two different things. System.Web.HttpContext and the static property: System.Web.Mvc.Controller.HttpContext (Which does not have a "Current" property.
-
mawaldne over 14 years"or you could just call User as I think it's a property of ViewPage" - So do you mean use Page.user when you're in the view?
-
Wagner Danda da Silva Filho almost 14 yearsThat worked on a non-MVC environment, just what I needed. Thanks! :)
-
jamiebarrow over 13 yearsIt's useful in any ASP.NET application that uses the Membership providers.
-
Mike Cole over 11 yearsThis will actually make a database request. HttpContext.Current.User doesn't.
-
Jed Grant almost 11 yearsUnfortunately this doesn't seem to work anymore in MVC 5. Not sure why =/
-
Sean almost 10 yearsYes, you can use it like, "Hi, @User.Identity.Name!" in the cshtml.
-
Paul over 9 yearsJust to add to this, if you're working in a class outside of a form you'll either need to
Imports System.Web
and further qualify with withHttpContext.Current.User.Identity.Name
, or directly qualify using the full syntax:System.Web.HttpContext.Current.User.Identity.Name
-
ryanyuyu almost 9 yearsWhile this code may answer the question, it would be better to include some context, explain how it works, and describe when to use it. Code-only answers are not useful in the long run.
-
GrandMasterFlush almost 9 yearsSystem.Security.Principal.WindowsIdentity.GetCurrent().Name returns the current user logged into Windows, the OP is asking for the user currently logged into the web site.
-
Simple Sandman about 8 yearsIf you are getting this error
'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found
, I would suggest making an absolute call like this,System.Web.HttpContext.Current.User.Identity.Name
. -
usefulBee almost 8 yearsThis works only inside a Controller; or if your ViewModel inherits from Controller
-
usefulBee almost 8 yearsWorks inside a Controller and if using a ViewModel either inherit from Controller or follow Paul's suggestion.
-
PineCone almost 8 yearsOnly
System.Security.Principal.WindowsIdentity.GetCurrent().Name
works inMVC 5
. However that pulls up the domain name with the username. i.e. domain\username -
Beau D'Amore over 7 yearsI do not believe so, as this is explicitly using your Active Directory user credentials. Why would you have 'Forms' if you desire your users to be authenticated via AD?
-
Patee Gutee over 7 yearsIn our organization, there are locations where we have a couple of workstations that are shared by several "field" personnel. Because of that, we are required to add the login page to our intranet web applications.
-
Beau D'Amore over 7 yearsas a workaround: you could have two copies of this app running, one in 'Forms' mode with it's own identity tables or you can mix both in one app, but it's trickier. A quick google revealed this: stackoverflow.com/questions/2250921/…
-
Beau D'Amore over 7 years<identity impersonate="true" /> might need to change if mixing
-
Gilberto B. Terra Jr. over 7 yearsThis method return a System.Guid
-
Samra almost 7 yearsUser.Identity.Name is an instance of System.Security.Principal.IPrincipal which does not have an id property... Is this User different from User.Identity.GetUserId 's User object
-
Jason Geiger almost 5 yearsThis gets you the username of the AppPool that the application is running as.
-
rickyProgrammer over 3 yearshi @rajbaral This worked for me, what if i want to extract the other information from the current user from the db, example address field.
-
Raj Baral over 3 years@rickyProgrammer you need to write a method to get UserProfile from your AD using that
userName
-
Lemiarty about 2 yearsvar userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; This is just straight up incorrect information. This call will get you the identity of the Application Pool and not of the web user.