I can get the valid user from the database, create the ClaimsIdentity
and the SignIn
method is called without error.
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();
}
However, when I check if the user is signed in on the view like this:
<p>
Current User: @if (User.Identity.IsAuthenticated)
{
@User.Identity.Name
}
else
{
@:Unknown
}
</p>
IsAuthenticated
returns false
.
I was missing the AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
from the OWIN startup class:
var authenticationOptions = new CookieAuthenticationOptions
{
LoginPath = new PathString("/Account/SignIn"),
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
};
appBuilder.UseCookieAuthentication(authenticationOptions);
It's a shame that there isn't a nice, useful error. I don't like programs that silently fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With