4

我想知道用户身份名称是什么设置并更改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);
    }
}
4

1 回答 1

5

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

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

UserManager.FindByNameAsync(model.Name)

于 2015-06-08T08:49:10.197 回答