ASP.NET MVC - 登录成功,但userId返回null

时间:2015-10-26 10:53:47

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-identity identity

我正在使用基本的mvc-5登录方法,只是尝试在成功时访问用户ID

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            string userId = User.Identity.GetUserId();
            returnUrl = CheckUserRoleAndRedirect();
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

此处string userId = User.Identity.GetUserId(); userId第一次返回null,但下次工作正常

2 个答案:

答案 0 :(得分:7)

登录后(并重定向到其他页面),IPrincipal.IIdentity应为ClaimsIdentity。你可以试试这个:

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;
    }
}

这对你有用。如果仍然无法获取id,则必须在服务器将身份验证cookie写入浏览器之前重定向到另一个页面。

或另一种方法是:

switch (result)
{
    case SignInStatus.Success:
       ApplicationUser user = UserManager.FindByName(model.UserName);
       string UserId = user.Id;
       returnUrl = CheckUserRoleAndRedirect();
       return RedirectToLocal(returnUrl);
}

答案 1 :(得分:5)

一旦调用,SignInAsync会创建一个AuthenticationResponseGrant,并且在下次调用之前,User.Identity中不会提供详细信息。

但是你可以直接访问AuthenticationResponseGrant.Identity来解决这个问题:

var Grant = SignInManager.AuthenticationManager.AuthenticationResponseGrant;
string UserId = Grant.Identity.GetUserId();

当然不需要映射Grant但是它使代码更具可读性。