表单身份validation超时与会话超时

在我的asp.net网站,我正在使用asp.net表单身份validation与以下configuration

<authentication mode="Forms"> <forms loginUrl="~/Pages/Common/Login.aspx" defaultUrl="~/Pages/index.aspx" protection="All" timeout="30" name="MyAuthCookie" path="/" requireSSL="false" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" > </forms> </authentication> 

我有以下问题

  1. 什么应该是会话的超时值,因为我使用表单validation内的滑动过期,因为在表单validation之前会话会过期。 我如何保护它?

  2. 在formauthentication注销后,我想在logout.aspxredirect页面,但它会自动redirect到loginpage.aspx。 这怎么可能?

  1. 为了安全起见:TimeOut(Session)<= TimeOut(FormsAuthentication)* 2
  2. 如果您想在authentication超时后显示除loginUrl属性中指定的页面,则需要手动处理,因为ASP.NET不提供这种方法。

要实现#2,您可以手动检查Cookie及其AuthenticationTicket过期,并在过期后redirect到您的自定义页面。
您可以在其中一个事件中执行: AcquireRequestState , AuthenticateRequest 。

事件中的示例代码可能如下所示:

 // Retrieve AuthenticationCookie var cookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) return; FormsAuthenticationTicket ticket = null; try { ticket = FormsAuthentication.Decrypt(cookie.Value); } catch (Exception decryptError) { // Handle properly } if (ticket == null) return; // Not authorised if (ticket.Expiration > DateTime.Now) { Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here } 

对于具有会话依赖性的站点,您可以简单地使用global.asax中的会话启动事件注销一个过时的身份validation:

 void Session_Start(object sender, EventArgs e) { if (HttpContext.Current.Request.IsAuthenticated) { //old authentication, kill it FormsAuthentication.SignOut(); //or use Response.Redirect to go to a different page FormsAuthentication.RedirectToLoginPage("Session=Expired"); HttpContext.Current.Response.End(); } } 

这使得新的会话=新的authentication,时期。