asp.net-mvc-3 – 超時在ASP.Net MVC FormsAuthentication中不起作用
我正在使用FormsAuthentication,我在設定TimeOut值時遇到問題.
我看過一些與此有關的其他帖子,但它們似乎並不完全符合我的問題,或者解決方案沒有幫助.
我的web.config具有以下內容:
<authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="1" cookieless="UseCookies" /> </authentication>
我把一個AuthorizeAttribute放在我想要保護的控制器上.
我可以檢視.ASPXAUTH cookie(使用FireCookies),我可以看到它設定為在登入後1分鐘內到期.
如果我除錯我的程式碼,我可以看到FormsAuthentication.Timeout = 1.
不過我的門票在1分鐘內似乎沒有超時. 1分鐘不活動後,我仍然可以使用AuthorizeAttribute瀏覽到控制器.
實際上我實際上可以使用FireCookie刪除.ASPXAUTH cookie,我仍然可以使用AuthorizeAttribute瀏覽到控制器.
在很長一段時間後,很奇怪的(抱歉沒有一個確切的時間 – 我出去吃午飯了)TimeOut發生了,我被重定向
到登入螢幕.
有任何想法嗎?
我也有同樣的問題.其實呢,是因為我無法從javascript讀取表單認證cookie,這是一段時間未定義的.我只想知道我是否通過javascript進行身份驗證.
後來我發現這張票已經過期,但我沒有登出(也是,所以我也想解決)!我看到你的問題沒有得到回答,所以當我解決問題半天的時候,我保持開放.以下是我想出的,似乎是工作.
我的答案是基於這個答案.ofollow,noindex" target="_blank">http://stackoverflow.com/a/454639/511438 由使用者ScottS
這是在我的ASP.NET MVC 3專案.
這是我的登入程式碼.未顯示,自定義使用者認證邏輯之前.這只是設定初始票.
public class FormsAuthenticationService:IFormsAuthentication
public void SignIn(string userName, bool createPersistentCookie, string role) { FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1,// version userName,// user name DateTime.Now,// created DateTime.Now.Add(FormsAuthentication.Timeout),// expires false,// rememberMe? role// can be used to store roles ); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Current.Response.Cookies.Add(authCookie); }
在同一個類中,但是從global.asax訪問的靜態方法
//-- this is based on http://stackoverflow.com/questions/454616/asp-net-cookies-authentication-and-session-timeouts internal static FormsAuthenticationTicket RefreshLoginCookie(bool retainCurrentExpiry) { HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null || authCookie.Value == null) return null; FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(authCookie.Value); DateTime expiryDate = (retainCurrentExpiry ? oldTicket.Expiration : DateTime.Now.Add(FormsAuthentication.Timeout)); HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName); var newTicket = new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, oldTicket.IssueDate, expiryDate, oldTicket.IsPersistent, oldTicket.UserData, oldTicket.CookiePath); HttpCookie newAuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket)); HttpContext.Current.Response.Cookies.Add(newAuthCookie); return newTicket; }
Global.asax中
我的自定義是ajax請求不重新整理表單身份驗證票證.所以如果你坐在那裡的超時時間,一個ajax請求將會登出你.更改,如果你想要ajax請求保持機票活著(解決我的JavaScript cookie問題,而不是你的登出不活動問題).
*(提示,如果您登出,然後登入,但再次回到登入頁面,第一次登入也沒有在querystring中指定一個returnUrl). *
protected virtual void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null || authCookie.Value == "") { return; } bool isAjax = new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest(); FormsAuthenticationTicket authTicket; try { //-- THIS IS WHAT YOU WANT authTicket = FormsAuthenticationService.RefreshLoginCookie(isAjax); } catch { return; } string[] roles = authTicket.UserData.Split(';'); if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity, roles); }
Web.config檔案
這裡是我設定會話超時和機票超時的部分
<configuration> <system.web> <sessionState mode="InProc" timeout="60" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="60" name="ProviderMvcSession" cookieless="UseCookies" /> </authentication>
http://stackoverflow.com/questions/8989749/timeout-not-working-in-asp-net-mvc-formsauthentication