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

Ari*_*jee 3 c# asp.net-mvc identity asp.net-mvc-5 asp.net-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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Rah*_*ate 7

登录后(并重定向到另一个页面),IPrincipal.IIdentity应该是a 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

或者另一种方法是:

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


Ste*_*ner 6

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

但是,您可以通过直接访问AuthenticationResponseGrant.Identity来解决此问题:

var Grant = SignInManager.AuthenticationManager.AuthenticationResponseGrant;
string UserId = Grant.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)

当然,没有必要映射,Grant但它使代码更具可读性.