2011-02-03 40 views
8

Đây là chức năng của tôi được gọi khi đăng nhập thành công. (Tôi rất mới để điều FormAuthentication này)FormsAuthenticationTicket hết hạn quá sớm

public static void CreateLoginCookie(User u) 
{ 
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(u.Id.ToString(), true, 9*60); 
    string encryptedTicket = FormsAuthentication.Encrypt(ticket); 
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = DateTime.Now.AddHours(9) }; 
    HttpContext.Current.Response.Cookies.Add(cookie); 
} 

Trong web.config Tôi có

<authentication mode="Forms"> 
    <forms loginUrl="~/Default/Login" timeout="540" /> 
</authentication> 

Tôi muốn nghỉ dùng đăng nhập trong 9 giờ, nhưng nó không hoạt động. Họ bị đăng xuất sau một hoặc hai giờ.

Ai đó có thể cho tôi biết những gì tôi đang thiếu?

+2

Bạn có chắc chắn đó là vé và không phải là phiên đó là hết hạn? –

Trả lời

1

Bạn đã xem xét sửa đổi thời gian chờ trong tệp web.config chưa?

<forms 
    name="name" 
    loginUrl="URL" 
    defaultUrl="URL" 
    protection="[All|None|Encryption|Validation]" 
    timeout="[MM]" 
    path="path" 
    requireSSL="[true|false]" 
    slidingExpiration="[true|false]"> 
    enableCrossAppRedirects="[true|false]" 
    cookieless="[UseUri|UseCookies|AutoDetect|UseDeviceProfile]" 
    domain="domain name" 
    ticketCompatibilityMode="[Framework20|Framework40]"> 
    <credentials>...</credentials> 
</forms> 
+0

Có, cảm ơn, tôi đã có 540 là thời gian chờ nhưng nó không hoạt động. Tôi đã cập nhật câu hỏi. – Aximili

+0

Tôi có một suy nghĩ. Bạn đang sử dụng SSL? – Victor

+0

Không, không có SSL tại đây – Aximili

1

Tôi đã sử dụng đoạn mã này và nó làm việc cho tôi, hãy xem này:

 FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket( 
               1,          // Ticket version 
               username,         // Username associated with ticket 
               DateTime.Now,        // Date/time issued 
               DateTime.Now.AddDays(1),     // Date/time to expire 
               isPersistent,        // "true" for a persistent user cookie 
               dataStore,        // User-data, in this case the roles 
               FormsAuthentication.FormsCookiePath);  // Path cookie valid for 

     // Encrypt the cookie using the machine key for secure transport 
     string Hash = FormsAuthentication.Encrypt(Ticket); 
     HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, Hash); 

     // Set the cookie's expiration time to the tickets expiration time 
     if (Ticket.IsPersistent) 
      Cookie.Expires = Ticket.Expiration; 
3

Nó có thể xảy ra vì tái chế Application Pool.

Cookie xác thực được mã hóa bằng các phím máy. Dường như theo mặc định, các phím máy này được tạo tại mỗi lần khởi động lại ứng dụng. Sau đó, ứng dụng của bạn không hoạt động trong một thời gian (được định cấu hình trong cài đặt nhóm ứng dụng), hồ bơi ứng dụng của bạn được tái chế.

Vì vậy, bạn cần tạo các khóa máy tĩnh.

Câu hỏi này có liên quan đến bạn: Can a FormsAuthenticationTicket survive an app pool recycle?