[英]ASP.NET Forms Authentication Expiry
有人可以向我解釋ASP.NET Forms身份驗證的工作原理,因為我似乎沒有得到它並且我一直在退出。
目前,我有用戶名,密碼和“保持登錄狀態”復選框。 從這些值我創建一個票證和cookie,如下所示:
// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,email,DateTime.UtcNow,DateTime.UtcNow.AddMinutes(30),remember,String.Empty);
// Encrypt ticket
string cookie_contents = FormsAuthentication.Encrypt(ticket);
// Create cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookie_contents);
if (remember) {
cookie.Expires = DateTime.UtcNow.AddDays(90);
}
cookie.Path = FormsAuthentication.FormsCookiePath;
cookie.Secure = true;
// Add cookie to response
Response.Cookies.Add(cookie);
我希望通過此代碼我可以登錄我的網站,並假設我檢查“保持登錄狀態”,我保持登錄狀態至少90天?
然而,我所看到的是,我是在首次登錄后至少30分鍾退出(這是留給機票的時間?)。
Cookie到期和故障單到期之間有什么區別,我如何保持自己的簽名。 我是否需要為cookie和票證設置90天?
如果可以避免,請不要直接操縱cookie。 您可以使用FormsAuthentication.SetAuthCookie(username, persistent)
對用戶進行簽名。此處持久表示“不使用會話cookie”。
然后,您應該在web.config中指定cookie到期時間
<system.web>
<authentication mode="Forms">
<forms timeout="50000000" slidingExpiration="true"/>
</authentication>
</system.web>
滑動到期意味着將為每個請求續訂cookie。 超時是幾分鍾,所以這個例子非常高:)
看一下這個問題以及Scott Gu博客的鏈接: Forms Authentication Cookie Expiration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.