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

rad*_*byx 4 asp.net asp.net-mvc iprincipal iidentity asp.net-identity

我想知道什么是用户身份名称,然后更改isAuthenticated为true。

为什么是User.Identity.Name空字符串,User.Identity.IsAuthenticated false之后SignInManager.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*nko 5

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

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

UserManager.FindByNameAsync(model.Name)