2011-02-03 5 views
8

이것은 로그인이 성공하면 호출되는 내 함수입니다. 내가FormsAuthenticationTicket이 너무 빨리 만료됩니다.

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

내가 사용자의 체류를 9 시간에 로그인 할 가지고있는 Web.config의에서

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); 
} 

(I이 FormAuthentication 것은 매우 새로운 오전),하지만 작동하지 않습니다. 1 시간이나 2 시간 후에 로그 아웃됩니다.

누군가 내가 누락 된 내용을 말해 줄 수 있습니까?

+2

는 당신이 티켓이 아닌 만료되는 세션의 확신 :

이 질문은 당신과 관련이? –

답변

1

web.config 파일에서 시간 초과를 수정 한 적이 있습니까?

<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

예, 시간 초과로 540을 얻었지만 작동하지 않았습니다. 나는 그 질문을 갱신했다. – Aximili

+0

나는 생각이있다. SSL을 사용하고 있습니까? – Victor

+0

아니요, 여기 SSL이 없습니다. – Aximili

1

나는이 조각을 사용했습니다 그것은 나를 위해 작동, 이것 좀보세요 :

 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

이 때문에 응용 프로그램 풀 재활용 발생할 수 있습니다.

인증 쿠키는 컴퓨터 키를 사용하여 암호화됩니다. 기본적으로 이러한 컴퓨터 키는 각 응용 프로그램 풀 다시 시작시 생성됩니다. 그런 다음 응용 프로그램 풀이 재활용되도록 얼마 동안 응용 프로그램 풀 설정에서 구성한 응용 프로그램이 유휴 상태입니다.

정적 컴퓨터 키를 생성해야합니다. Can a FormsAuthenticationTicket survive an app pool recycle?

관련 문제