2012-03-27 88 views
7

我已經將cookie過期時間設置爲1個月,但是當我在瀏覽器中查看.ASPXAUTH cookie的過期超時時,它表示從現在開始提前30分鐘。ASPX授權cookie過期時間總是30分鐘

var ticket = new FormsAuthenticationTicket(1, "myname", DateTime.Now, 
                 DateTime.Now.AddMonths(1), true, "test"); 
string ticketString = FormsAuthentication.Encrypt(ticket); 
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketString) 
       { 
        Expires = DateTime.Now.AddMonths(1), 
        Path = FormsAuthentication.FormsCookiePath 
       }; 
HttpContext.Current.Response.Cookies.Add(cookie); 

你能讓我知道爲什麼上面的代碼表現如此,我想改變過期時間,但它總是會在30分鐘後到來。

+0

你在哪裏指定了1個月? – V4Vendetta 2012-03-27 12:22:57

+0

您是否有任何理由手動創建身份驗證票證?如果您自動執行此操作,則可以通過web.config處理到期時間。 – 2012-03-27 12:28:12

+0

V4Vendetta他確實在這裏: 「DateTime.Now.AddMonths(1)」 落基辛格,你檢查slidingExpiration = 「假」 的屬性? 我猜你沒有把它指定爲false或者它不存在。 在這種情況下,每個請求重置期滿Web.Config中 – 2012-03-27 12:30:57

回答

2

檢查你的web.config文件,應該有以下元素的System.Web下FORM項 - >認證。

檢查那裏的超時屬性,是否設置爲30分鐘?

從那裏刪除此表單身份驗證標記。

+0

沿着它僅僅是<認證模式=「形式「> <形式loginUrl =」 login.aspx的「/> 2012-03-27 13:08:00

+0

嘗試域添加到形式標籤(任何字符串),I有同樣的問題,我不得不將其設置爲240分鐘時,我提出既該條目與web.config中的代碼和表單標籤中的添加域相同。我也在IIS中將應用程序池超時設置爲240分鐘。 – 2012-03-27 14:00:34

2

與其他的答案我到this鏈接的建議。

顯然,ASP.NET會檢查在Web.config到期,並沒有考慮從cookie到期。所以你需要添加到配置文件裏面<system.web>

<authentication mode="Forms"> 
    <forms 
name=".ASPXAUTH" 
loginUrl="Login.cshtml" //your login page 
defaultUrl="Default.cshtml" //your default page 
protection="All" //type of encryption 
timeout="43200" //a month in minutes 
path="/" 
requireSSL="false" 
slidingExpiration="true" //Every refresh the expiration time will reset 
cookieless="UseDeviceProfile" //Use cookies if the browser supports cookies 
domain="" 
enableCrossAppRedirects="false"> 
    <credentials passwordFormat="SHA1" /> 
    </forms> 
</authentication>