14

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.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
user2376512
  • 885
  • 1
  • 10
  • 21
  • Do you have your site setup for Windows auth? – Daniel Sanchez Aug 22 '14 at 04:14
  • @Daniel Sanchez No. – user2376512 Aug 22 '14 at 04:21
  • Read this article http://stackoverflow.com/questions/8810496/forms-authentication-understanding-context-user-identity Please set cookies first – Nitin Chaurasia Aug 22 '14 at 06:00
  • I was trying to do the exact same thing. Nitin is probably correct, but this link http://stackoverflow.com/questions/14508495/user-identity-isauthenticated-returns-false-after-setting-cookie-and-validating?rq=1 clarified it more for me in saying "User.Identity.IsAuthenticated won't be set to true until the next request after calling FormsAuthentication.SetAuthCookie()" (In case someone else comes along with the same issue.) – Joel Oct 10 '14 at 03:47
  • I have this same problem. I copied the boilerplate code and changed very little. When I run the boilerplate Login action everything works as predicted. When I try my copied action I get what happens to OP. – Jordan Apr 28 '15 at 20:03
  • see variable "scope". i believe it's a scope issue. – Sam Dec 16 '16 at 10:00

3 Answers3

17

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
}
Dewey
  • 904
  • 1
  • 10
  • 21
13

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.

GeoffM
  • 1,603
  • 5
  • 22
  • 34
0

Worked with me after tagging the method with [Authorize] attribute and sending the access-token in the authorize header, it seems that the access-token is needed to recognize the user

GH.Ezzat
  • 21
  • 7