Get UserID of logged-in user in Asp.Net MVC 5

79,409

The answer is right there in your code. What does this return?

var userID = User.Identity.GetUserId();

If you are using ASP.NET Identity, then after logging in (and redirecting to another page), the IPrincipal.IIdentity should be a ClaimsIdentity. You can try this:

var claimsIdentity = User.Identity as ClaimsIdentity;
if (claimsIdentity != null)
{
    // the principal identity is a claims identity.
    // now we need to find the NameIdentifier claim
    var userIdClaim = claimsIdentity.Claims
        .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);

    if (userIdClaim != null)
    {
        var userIdValue = userIdClaim.Value;
    }
}

The above block of code is not exactly, but essentially, what the IIdentity.GetUserId extension method does.

If none of this works, then the user may not really be logged into your site yet. After logging in, you have to redirect to another page before the server will write the authentication cookie to the browser. This cookie must be written before the User.Identity has all of this claims information (including the NameIdentifier) on subsequent requests.

Share:
79,409

Related videos on Youtube

Philipp Eger
Author by

Philipp Eger

Updated on July 09, 2022

Comments

  • Philipp Eger
    Philipp Eger almost 2 years

    I'm relatively new to ASP.Net MVC and try to use the built-in user login functionality now. I'm able to register an user in the registration view. If I try to login with the created user this also works. I'm redirected to the master page.

    But I'm not able to get the UserID of the current user. I tried my code in the HomeController and in the AccountController, but both didn't work. The statement in the first line returns always null.

    var userID = User.Identity.GetUserId();
    
    if (!string.IsNullOrEmpty(userID))
    {
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create()));
        var currentUser = manager.FindById(User.Identity.GetUserId());
    }
    

    Do I have to to something else before getting the UserID?

  • Philipp Eger
    Philipp Eger over 9 years
    Sorry, that was not clear in my question, I'll change it. The statement above returns null.
  • Chris Pratt
    Chris Pratt over 9 years
    If User.Identity.GetUserId() returns null, that means the user is not logged in. Make sure whatever action you're calling this in is decorated with [Authorize], to enforce that only logged in users can get to it. Anyone not logged in will be sent automatically to your login page.
  • Philipp Eger
    Philipp Eger over 9 years
    Thank you for your answer. The hint that I've to redirect to another page before this works helped.
  • Joshua Ohana
    Joshua Ohana about 9 years
    @ChrisPratt I am having the same problem. GetUserId() always returns null. I have [Authorize] on my controller and am passing along the Bearer token with my request. Any ideas?
  • Yiping
    Yiping over 8 years
    " then after logging in (and redirecting to another page)" so before redirecting, inside the controller it is always null?
  • ThiagoAlves
    ThiagoAlves almost 8 years
    You have to add this to use the extension method: using Microsoft.AspNet.Identity;
  • Zapnologica
    Zapnologica almost 8 years
    How would one get user ID, in the Login Action, Do I have to use email address?