6
Mogę pobrać prawidłowego użytkownika z bazy danych, utworzyć metodę ClaimsIdentity
i wywołać metodę SignIn
bez błędu.AuthenticationManager.SignIn() nie rejestruje się w
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();
}
Jednak, kiedy sprawdzić, czy użytkownik jest zalogowany na widoku tak:
<p>
Current User: @if (User.Identity.IsAuthenticated)
{
@User.Identity.Name
}
else
{
@:Unknown
}
</p>
IsAuthenticated
powraca false
.