How to get the current user in ASP.NET MVC

390,249

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.HttpContext

Summary:
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.
Share:
390,249

Related videos on Youtube

Serhat Ozgel
Author by

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, 2022

Comments

  • Serhat Ozgel
    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
    Serhat Ozgel over 15 years
    Where does this Membership class come from? IntelliSense does not recognize it by default.
  • Serhat Ozgel
    Serhat Ozgel over 15 years
    Apparently HttpContext does not have a property named "Current".
  • Sean
    Sean over 15 years
    The 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
    Jeff Sheldon over 15 years
    I 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
    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
    Wagner Danda da Silva Filho almost 14 years
    That worked on a non-MVC environment, just what I needed. Thanks! :)
  • jamiebarrow
    jamiebarrow over 13 years
    It's useful in any ASP.NET application that uses the Membership providers.
  • Mike Cole
    Mike Cole over 11 years
    This will actually make a database request. HttpContext.Current.User doesn't.
  • Jed Grant
    Jed Grant almost 11 years
    Unfortunately this doesn't seem to work anymore in MVC 5. Not sure why =/
  • Sean
    Sean almost 10 years
    Yes, you can use it like, "Hi, @User.Identity.Name!" in the cshtml.
  • Paul
    Paul over 9 years
    Just 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 with HttpContext.Current.User.Identity.Name, or directly qualify using the full syntax: System.Web.HttpContext.Current.User.Identity.Name
  • ryanyuyu
    ryanyuyu almost 9 years
    While 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
    GrandMasterFlush almost 9 years
    System.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
    Simple Sandman about 8 years
    If 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
    usefulBee almost 8 years
    This works only inside a Controller; or if your ViewModel inherits from Controller
  • usefulBee
    usefulBee almost 8 years
    Works inside a Controller and if using a ViewModel either inherit from Controller or follow Paul's suggestion.
  • PineCone
    PineCone almost 8 years
    Only System.Security.Principal.WindowsIdentity.GetCurrent().Name works in MVC 5. However that pulls up the domain name with the username. i.e. domain\username
  • Beau D'Amore
    Beau D'Amore over 7 years
    I 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
    Patee Gutee over 7 years
    In 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
    Beau D'Amore over 7 years
    as 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
    Beau D'Amore over 7 years
    <identity impersonate="true" /> might need to change if mixing
  • Gilberto B. Terra Jr.
    Gilberto B. Terra Jr. over 7 years
    This method return a System.Guid
  • Samra
    Samra almost 7 years
    User.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
    Jason Geiger almost 5 years
    This gets you the username of the AppPool that the application is running as.
  • rickyProgrammer
    rickyProgrammer over 3 years
    hi @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
    Raj Baral over 3 years
    @rickyProgrammer you need to write a method to get UserProfile from your AD using that userName
  • Lemiarty
    Lemiarty about 2 years
    var 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.