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

What set's the User.Identity.Name and User.Identity.IsAuthenticated?

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

为什么 User.Identity.Name 是空字符串,而 User.Identity.IsAuthenticated falseSignInManager.PasswordSignInAsync 返回 Success

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

似乎 SignInManager.PasswordSignInAsync 只验证输入的数据,运行 AuthenticationManager.SignIn 如果您不使用 TwoFactorAuthenticationAuthenticationManager.SignIn 在这种情况下仅将身份验证 cookie 设置为响应。

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

UserManager.FindByNameAsync(model.Name)