2017-04-19 108 views
1

我在一個ASP MVC登錄表單上工作。爲什麼我的microsoft.owin.security.authenticationmanager登錄方法不起作用?

我有非常簡單的代碼。 A Startup類和嘗試設置Cookie的操作。下面是我的代碼:

啓動 它位於App_Start(也有與key="owin:AppStartup"對它的引用在<appSetting>

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "ApplicationCookie", 
      LoginPath = new PathString("/auth/login"), 
     }); 
    } 
} 

被假設驗證用戶是該動作的方法:

[HttpPost] 
public ActionResult Login(user model) 
{ 
    if(ModelState.IsValid) 
    { 
     var identity = new ClaimsIdentity(new[] 
     { 
      new Claim(ClaimTypes.Email, "[email protected]"), 
      new Claim(ClaimTypes.Name, "tom"), 
      new Claim(ClaimTypes.Role, "admin") 
     }); 

     var ctx = Request.GetOwinContext(); 
     var authManager = ctx.Authentication; 
     authManager.SignIn(identity); 
     return RedirectToAction("Index", "Home"); 
    } 
    return View(model); 
} 

但這沒有得到身份認證爲@User.Authenticated在我的_Layout.cshtml中是錯誤的,當return RedirectToAction("Index", "Home");和debbuger顯示IsAuthenticated屬性爲false(在控制器Login動作中和在_Layout.cshtml中。

我已檢查IIS是否爲匿名身份驗證使用我的Windows管理工具啓用,還我已經檢查了啓動設置應用程序啓動時...

我看來,authManager.SignIn(identity)沒有做的工作。

我們該如何解決?

debugger screenshot

PS:我甚至不看到瀏覽器彈出,詢問我是否保存密碼

回答

1

簽到仍然存在(我在我的測試,即使用戶仍然沒有通過身份驗證彈出只有一次)用戶未來的請求(通過cookie),它不會改變當前的請求。如果需要,您可以直接爲當前請求設置HttpContext.User。

我還記得您需要將ClaimsIdentity AuthenticationType設置爲CookieAuthenticationDefaults.AuthenticationType(或您用於識別中間件的任何身份驗證類型)。否則cookie認證中間件將不會激活。

相關問題