Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AuthenticationManager.SignIn() is not signing in

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.

like image 358
BanksySan Avatar asked Oct 25 '15 14:10

BanksySan


1 Answers

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.

like image 92
BanksySan Avatar answered Oct 22 '22 13:10

BanksySan