2014-12-11 93 views
0

默認情況下,Identity 2.1身份驗證Cookie到期時間通過Startup.Auth.cs通過CookieAuthenticationOptions全局設置。 有沒有辦法做到這一點,取決於用戶,所以它可以單獨配置?可更改的cookie到期時間

回答

2

是的,這是可能的。使用AuthenticationProperties指定在您撥打SignIn時的到期時間,就像這樣。

var claims = new List<Claim>() { new Claim(ClaimTypes.Name, "Alice") }; 
var identity = new ClaimsIdentity(claims, "ApplicationCookie"); 

var properties = new AuthenticationProperties() 
{ 
    ExpiresUtc = DateTimeOffset.Now.AddDays(1) // One day expiry for Alice 
}; 

Request.GetOwinContext().Authentication.SignIn(properties, identity); 
+0

謝謝,這就是我一直在尋找的。然而,新的Identity'SignInManager'有一個問題:重寫'AuthenticationProperties'需要重載'SignInAsync',這是不可插入的。所以需要驗證每個新版本的覆蓋! – 2014-12-11 05:11:34