【问题标题】:Differences in forms auth timeout and session timeout表单认证超时和会话超时的区别
【发布时间】:2011-01-11 19:11:45
【问题描述】:

使用此 web.config 元素设置会话状态超时

<sessionState mode="InProc" cookieless="false" timeout="120" />

使用此 web.config 元素配置表单身份验证

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>
</system.web>

每个元素中指定的超时之间有什么区别?如果两者不同,它会如何工作?

【问题讨论】:

    标签: asp.net authentication forms-authentication


    【解决方案1】:

    每次有新用户访问网站时都会启动会话,无论他们是否匿名。身份验证与 Session 关系不大。

    身份验证超时是身份验证 cookie 在用户浏览器上的有效时间。一旦 cookie 过期,他们必须重新进行身份验证才能访问网站上的受保护资源。

    因此,如果 Session 在 Authentication cookie 之前超时 - 它们仍然经过身份验证,但它们的所有会话变量都消失了,并且如果您在检查空值和其他因缺少会话而导致的情况时不遵守纪律,可能会导致您的网站出错.

    如果 Authentication 在会话之前超时,那么他们的所有会话变量仍然存在,但他们将无法访问受保护的资源,直到他们重新登录。

    【讨论】:

    • 很好的解释。我只想补充一点,IIS 中的应用程序池级别存在另一个重要的超时设置。 IIS 将在达到指定的空闲超时后重新启动池,以便释放分配的资源。您应该确保此池空闲超时始终大于上述两个超时,否则无论会话或表单超时设置为什么,您都会收到错误。
    • 确实,很好的解释。就像@learner一样,我想提另一个设置,除了idle time-outRecycling worker process。默认情况下,这发生在 29 小时后,这不是滑动到期。如果您的会话状态模式为in process,则在发生回收时会话将被清除。
    【解决方案2】:

    正如预期的那样。

    例如如果您的会话在 20 分钟后超时,您的会话变量将丢失。 但用户可以访问受身份验证保护的页面。

    如果身份验证超时,用户将无法访问它所保护的页面,并且会话的状态无关紧要。

    【讨论】:

      【解决方案3】:

      会话超时值必须小于 FormsAuthentication 超时时间。因为 Session 可以由于某种原因而被删除,并且该场景将不起作用。

      如果会话超时,请检查 FormsAuthentication Ticket。如果票证有效且未超时,则在您的登录页面重新生成会话(在您的 web.config 文件中定义为 FormsAuthentication 设置的 LoginUrl 参数)。

      如果 FormsAuthentication 超时,ASP.NET FormsAuthentication 会自动将用户重定向到登录页面,并且用户必须再次登录。

      不要忘记当票证超时时 FormsAuthentication 会自动将用户重定向到登录页面。

      其实我更喜欢这种结构:

      1. 为需要登录的页面创建一个 BasePage.cs。
      2. 在 BasePage.cs 中的 Page_Init 检查会话。如果会话过期;将用户重定向到登录页面。

        if (Session["UserId"] == null) Response.Redirect("Login.aspx", true);

      3. 在 Login.aspx 的 Page_Load 中正确检查 Session 和 FormsAuthentication Ticket 时间。

            //User already have a session; redirect user to homepage. 
            if (SessionHandler.UserId != 0)
                Response.Redirect("HomePage.aspx");
            else 
            {
                //Session is killed; check Ticket is Valid or not
                if (Context.User.Identity != null && Context.User.Identity.IsAuthenticated)
                {
                    //Use Value of the FormsAuthentication
                    var customDataToCheck = Context.User.Identity.Name;
        
                    //Use value to check user is exist really and check db for user's session
                    var user = CheckUserData(customDataToCheck);
                    if (user != null)
                    {
                        //Start Session here 
                        SessionHandler.StartSession(user);
        
                        //Redirect user to page what you want. 
                        Response.Redirect("HomePage.aspx?ref=regenerated_session");
                    }                    
                }
            }
        
      4. 在 Login.aspx 中使用 FormsAuthentication 创建 cookie。

        if (用户名 == "testuser" && 密码 == "testpassword") { //用户数据将被写入cookie,存储用户数据,您可以检查用户是否有效(例如用户名或用户ID)。
        System.Web.Security.FormsAuthentication.SetAuthCookie("testUserData", keepMeSignedInCheckBox.Checked); Response.Redirect("HomePage.aspx"); }

      【讨论】:

        猜你喜欢
        • 2010-12-01
        • 2011-10-17
        • 2013-10-22
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 2013-11-06
        • 2011-04-24
        • 2018-01-28
        相关资源
        最近更新 更多