表单身份验证超时与会话超时

时间:2009-09-24 10:03:05

标签: asp.net forms session timeout asp.net-3.5

在我的asp.net网站上,我使用asp.net表单身份验证,配置如下

<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. 什么应该是会话的超时值,因为我在表单身份验证中使用滑动到期,因为在表单身份验证之前哪个会话将到期。我该如何保护它?

  2. 在formuthentication注销后,我想在logout.aspx重定向页面,但它会自动重定向到loginpage.aspx。怎么可能?

2 个答案:

答案 0 :(得分:50)

  1. 为安全起见:TimeOut(Session)&lt; = TimeOut(FormsAuthentication)* 2
  2. 如果要在身份验证超时后显示loginUrl属性中指定的页面之外的其他页面,则需要手动处理此页面,因为ASP.NET不提供此方法。
  3. 要实现#2,您可以手动检查Cookie及其AuthenticationTicket是否过期,如果过期,则会重定向到您的自定义页面。
    您可以在其中一项活动中执行此操作:AcquireRequestStateAuthenticateRequest

    活动中的示例代码如下所示:

    // 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
    }
    

答案 1 :(得分:24)

对于具有会话依赖性的站点,您只需使用global.asax中的会话启动事件退出过时的身份验证:

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

}

这使得新会话=新的身份验证,期限。

相关问题