我想知道用户身份名称的设置,并将isAuthenticated
更改为true。
为什么SignInManager.PasswordSignInAsync
返回Success
后User.Identity.Name
为空字符串,User.Identity.IsAuthenticated
为false
// 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)