How can I handle forms authentication timeout exceptions in ASP.NET?

前端 未结 3 1384
醉话见心
醉话见心 2020-11-29 07:03

If the session has expired and the user clicks on a link to another webform, the asp.net authentication automatically redirect the user to the login page.

However, t

3条回答
  •  误落风尘
    2020-11-29 07:45

    If you're using Forms Authentication, the user will be redirected to the login page when the Forms Authentication ticket expires, which is not the same as the Session expiring.

    You could consider increasing the Forms Authentication timeout if appropriate. Even to the extent of using a persistent cookie. But if it does expire, there's no real alternative to redirecting to the login page - anything else would be insecure.

    One way to deal with Session timeouts is to use Session as a cache - and persist anything important to a backing store such as a database. Then check before accessing anything in Session and refresh if necessary:

    MyType MyObject
    {
        get
        {
            MyType myObject = Session["MySessionKey"] as MyType
            if (myObject == null)
            {
                myObject = ... get data from a backing store
                Session["MySessionKey"] = myObject;  
            }
            return myObject;
        }
        set
        {
            Session["MySessionKey"] = value;
            ... and persist it to backing store if appropriate
        }
    }
    

提交回复
热议问题