Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Identity : User.Identity.GetUserId() is always null and User.Identity.IsAuthenticated is alway false

See my code below:

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
    case SignInStatus.Success:
        string UserId = User.Identity.GetUserId(); 
        return RedirectToAction("ClientDetails","Home");
    case SignInStatus.LockedOut:
        return View("Lockout");
    case SignInStatus.RequiresVerification:
        return RedirectToAction("SendCode", "Account", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
    case SignInStatus.Failure:
    default:
        ModelState.AddModelError("", "Invalid login attempt.");
        return View(model);
}

The UserId is always null and User.Identity.IsAuthenticated is always false. But I can view the View ClientDetails which requires authentication.

like image 439
user2376512 Avatar asked Aug 22 '14 03:08

user2376512


People also ask

Why is user identity Isauthenticated false?

identity. isauthenticated is False when a user is already logged in.

What is Microsoft ASP NET identity?

The ASP.NET Identity system is designed to replace the previous ASP.NET Membership and Simple Membership systems. It includes profile support, OAuth integration, works with OWIN, and is included with the ASP.NET templates shipped with Visual Studio 2013.

What is user identity in C#?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

What is ASP Net Identity in MVC?

ASP.NET Identity is the membership system for authentication and authorization of the users by building an ASP.NET application. The ASP.NET Identity is a fresh look at what the membership system should be when you are building modern applications for the web, phone or tablet.


2 Answers

I assume your example is the code from your AccountController.Login() method. I had the same problem as you but discovered that the User object won't be populated until the next request. Try this approach:

case SignInStatus.Success:
    return RedirectToAction("DoWork", "Account");


public async Task<ActionResult> DoWork()
{
    //this works
    string UserId = User.Identity.GetUserId();
    //return to View or Redirect again
}
like image 85
Dewey Avatar answered Sep 28 '22 04:09

Dewey


For the "The UserId is always null" part of the question, you can look up the user by the model.UserName:

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
    case SignInStatus.Success:
        ApplicationUser user = UserManager.FindByName(model.UserName);
        string UserId = user.Id;
        // UserId is now populated
        return RedirectToAction("ClientDetails","Home");

etc. Not sure if you wanted User.Identity.IsAuthenticated true or whether that was an observation - this doesn't change that part.

like image 24
GeoffM Avatar answered Sep 28 '22 04:09

GeoffM