[英]ASP.MVC Remember me cookie not working after session time out
在登錄表單上,我有一個選項允許用戶單擊“記住我”復選框,該復選框創建一個新的FormsAuthenticationTicket
,然后將其添加到Cookie中。
if (_model.RememberMe)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
_model.Username,
DateTime.Now,
DateTime.Now.AddDays(30),
true,
_model.Username,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
如上所述,希望可以在客戶端瀏覽器中保存30天。
測試一下,我故意將當前會話超時只保留了一分鍾
<sessionState timeout="1"></sessionState>
因此,過了一分鍾,如果用戶說“記住我”,我希望該網站不應重定向回登錄頁面。 但是確實如此。 這是執行此操作的代碼。
// [".ASPXAUTH"] is the cookie name that is created by the FormsAuthenticationTicket`
if (User.Identity.Name == "" && Request.Cookies[".ASPXAUTH"] == null)
{
return RedirectToAction("LogOut", "Login");
}
// the current session hasn't timed out or the remember me cookie is enabled
FormsIdentity id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
但是cookie是NULL。
我希望這對我來說是一個誤會,所以如果有人可以幫助我。 我會很感激。
謝謝
您正在尋找的是
string mySessionCookie = System.Web.HttpContext.Current.Request.Headers["Cookie"];
if (mySessionCookie.IndexOf(".ASPXAUTH", StringComparison.Ordinal) >= 0) {
// do something
}
編輯
怎么樣,我還沒有測試過,但是我記得以前做過這樣的事情
HttpCookie cookie = (HttpCookie)(Request.Cookies[FormsAuthentication.FormsCookieName]);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.