2015-10-25 37 views
6

我可以從數據庫獲取有效用戶,創建ClaimsIdentity,並且調用SignIn方法時沒有錯誤。AuthenticationManager.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

回答

7

我是缺少從OWIN啓動類的AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie

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

appBuilder.UseCookieAuthentication(authenticationOptions); 

這是一個恥辱,沒有一個很好的,有用的錯誤。我不喜歡那種默默失敗的節目。