Differences in forms auth timeout and session time

2019-01-16 16:22发布

问题:

The session state timeout is set using this web.config element

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

The forms auth is configured using this web.config element

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

What is the difference between the timeouts specified in each of these elements? If both are different, how would it work?

回答1:

A session starts every time a new user hits the website, regardless of whether or not they are anonymous. Authentication has very little to do with Session.

Authentication timeout is the amount of time that the authentication cookie is good for on the user's browser. Once the cookie expires, they must re-authenticate to access protected resources on the site.

So, if Session times out before the Authentication cookie - they are still authenticated, but all their session variables disappear, and may cause errors in your website if you are not disciplined in checking for nulls and other conditions brought about by missing session.

If Authentication times out before the session, then all their session variables will still exist, but they won't be able to access protected resources until they log back in again.



回答2:

as expected.

e.g. if your session times out after 20 minutes, your session-variables will be lost. but the user could access the pages which are protected by the authentication.

if the authentication times out, the user could not access the page which it protects, and the state of the session is irrelevant.



回答3:

Session Timeout value must be smaller than FormsAuthentication timeout time. Because Session can be removed because of a reason and the scenario won't work.

If Session times out, check FormsAuthentication Ticket. If ticket is valid and not time out, then re-generate session at your Login page (defined in your web.config file as LoginUrl parameter of FormsAuthentication settings).

If FormsAuthentication times out, ASP.NET FormsAuthentication redirects user automatically to Login Page and user has to login again.

Don't forget that FormsAuthentication automatically redirects user to login page when ticket time outs.

Actually I prefer this structure:

  1. Create a BasePage.cs for login required pages.
  2. at Page_Init in BasePage.cs check Session. If Session Expired; redirect user to Login page.

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

  3. At Page_Load in Login.aspx check Session and FormsAuthentication Ticket times properly.

        //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. at Login.aspx use FormsAuthentication to create cookies.

    if (username == "testuser" && password == "testpassword") { //User data will be written to a cookie, store a user data that you can check user is valid or not (for example Username or UserId).
    System.Web.Security.FormsAuthentication.SetAuthCookie("testUserData", keepMeSignedInCheckBox.Checked); Response.Redirect("HomePage.aspx"); }