2015-10-25 15 views
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.

Odpowiedz

7

mi brakowało AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie z klasy startowego OWIN:

var authenticationOptions = new CookieAuthenticationOptions 
{ 
    LoginPath = new PathString("/Account/SignIn"), 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie 
}; 

appBuilder.UseCookieAuthentication(authenticationOptions); 

To wstyd, że nie jest to miłe, przydatne błąd. Nie lubię programów, które po cichu zawodzą.

Powiązane problemy