2017-04-22 115 views
1

我用會員與此代碼來創建一個永久性的Cookie,以防止用戶自動登錄之後登出:如何自動防止註銷?

FormsAuthentication.SetAuthCookie(user.Id.ToString(),true); 

而且在web.config文件中使用此設置:

<authentication mode="Forms"> 
    <forms name=".mywebsite" cookieless="UseCookies" loginUrl="~/Account/SignIn" defaultUrl="~/ManagePanel/Statistic" slidingExpiration="true" protection="All" path="/" timeout="43200" /> 
</authentication> 
<sessionState mode="InProc" timeout="43200" /> 
<httpModules> 

<modules> 
    <remove name="FormsAuthenticationModule" /> 
    <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" /> 
    <remove name="UrlAuthorization" /> 
    <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" /> 
    <remove name="DefaultAuthentication" /> 
    <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" /> 
</modules> 

我希望用戶只需點擊註銷按鈕即可註銷,不想在一段時間後退出。

我該如何解決我的問題?

+0

目前,您已將窗體身份驗證cookie的有效性設置爲'43200'分鐘,且過期失效。這意味着如果在這段時間內沒有來自用戶的活動,則cookie將失效。這段時間不適合你嗎?或者您需要無限超時,以便cookie永不過期? –

+0

@DarinDimitrov這是一個足夠的時期,但不工作。例如,它在30分鐘後不到一個月後過期 – Mike

+0

我看到你也在使用''。您的身份驗證是否依賴於此會話中存儲的某些值?如果是這樣,那麼你不應該使用'InProc'。因爲這意味着您的會話值存儲在AppDomain的內存中。正如您所知,AppDomain可隨時由IIS卸載。例如,如果沒有任何活動或者達到某些CPU或內存水印。如果您希望它們能夠在AppDomain重新啓動(可以在任何時間,無法控制的情況下發生)下使用,您將不得不爲會話使用超出程序的存儲機制。 –

回答