我可以从数据库中获取有效的用户,创建ClaimsIdentity
,并且在调用SignIn
方法时没有错误发生。
public ActionResult SignInConformation(SignInModel model)
{
if (ModelState.IsValid)
{
var user = _userManager.Find(model.Username, model.Password);
if (user == null)
{
ModelState.AddModelError("", "Invalid username and\\or password");
}
else
{
_authenticationManager.SignOut();
var claimsIdentity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
_authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claimsIdentity);
return RedirectToAction("Index", "Home");
}
}
return View();
}
然而,当我在视图中检查用户是否已登录时,代码如下:
<p>
Current User: @if (User.Identity.IsAuthenticated)
{
@User.Identity.Name
}
else
{
@:Unknown
}
</p>
IsAuthenticated
返回 false
。