2011-02-03 18 views
8

これは、ログインに成功したときに呼び出される私の関数です。私はFormsAuthenticationTicketが期限切れになります

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

を持って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); 
} 

(私はこのFormAuthenticationの事に非常に新しいです)私は、ユーザーの滞在は9時間でログインしたいが、それは動作しません。 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

それが原因でアプリケーションプールのリサイクルの起こるかもしれません。

認証Cookieは、マシンキーで暗号化されています。 デフォルトでは、これらのマシンキーは、各アプリケーションプールの再起動時に生成されるようです。 次に、アプリケーションプールがリサイクルされている間、アプリケーションはしばらくアイドル状態です(アプリケーションプール設定で構成されています)。

静的マシンキーを生成する必要があります。

この質問はあなたに関連している: Can a FormsAuthenticationTicket survive an app pool recycle?

関連する問題