2011-02-03 12 views
8

यह मेरा कार्य है जिसे लॉगिन सफल होने पर कहा जाता है। (मैं इस FormAuthentication बात करने के लिए बहुत नया हूँ)फॉर्म प्रमाणीकरण टिकट जल्द ही समाप्त हो जाता है

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

web.config में मैं

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

मैं उपयोगकर्ता ठहरने 9 घंटे के लिए में लॉग इन करना चाहते हैं, लेकिन यह काम नहीं करता। वे एक या दो घंटे बाद लॉग आउट हो जाते हैं।

क्या कोई मुझे बता सकता है कि मुझे क्या याद आ रही है?

+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

मेरे पास एक विचार है। क्या आप एसएसएल का उपयोग कर रहे हैं? – Victor

+0

नहीं, यहां कोई एसएसएल नहीं है – 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?