User.Identity.Name 和 User.Identity.IsAuthenticated 是什么集?



我想知道用户身份名称的设置,并将isAuthenticated更改为true。

为什么SignInManager.PasswordSignInAsync返回SuccessUser.Identity.Name为空字符串,User.Identity.IsAuthenticatedfalse

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    var userIdentityNameTest = User.Identity.Name; // Empty string
    var result = await SignInManager.PasswordSignInAsync(
                                             model.Email, model.Password, 
                                             model.RememberMe, shouldLockout: false);
    // result is "Success"
    userIdentityNameTest = User.Identity.Name;
    // userIdentityNameTest is still an empty string?
    // User.Identity.IsAuthenticated is still false?
    switch (result)
    {
        case SignInStatus.Success:
            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);
    }
}

如果您不使用TwoFactorAuthentication, SignInManager.PasswordSignInAsync似乎只验证输入的数据并运行AuthenticationManager.SignIn。在本例中,AuthenticationManager.SignIn只将身份验证cookie设置为response。

因此,User.Identity在对应用程序的后续请求中可用。要通过Name获得ApplicationUser,您可以使用ApplicationUserManager,如下所示:

UserManager.FindByNameAsync(model.Name)

相关内容

  • 没有找到相关文章

最新更新