What is the most efficient way to handle en expired session with ASP.NET 2.0

假如想象 提交于 2019-12-05 20:04:42

We added the following code to the global.asax.cs file:

 private void IsAuthenticated()
    {
        string vFileName = Path.GetFileName(HttpContext.Current.Request.Path);
        string vExt = Path.GetExtension(vFileName).ToLower();
        if ((vFileName != "Login.aspx") && (vExt == ".aspx"))
        {
            if (HttpContext.Current.Session["LoggedIn"] == null)
            {
                HttpContext.Current.Response.Redirect("~/Login.aspx");
            }
        }
    }
    void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        IsAuthenticated();
    } 

NS: The first line in our Global .asax file is :

<%@ Application  Inherits="???.Global" Language="C#" %>

We handled it by checking if the session data existed in Application.Begin_Request for the pages that were user specific and if it didn't then redirecting the user to login or homepage.

You can use the session_end method, as it is not a user callable method, it is triggered by ASP.NET and response isn't available, as it is not part of the request.

The biggest way is to check and see if session is missing, somewhere in the load of your pages and redirect back to the main.

What I have done before is to put this checking logic inside a "RestrictedPage.master" master page that was used for all session specific pages, if session is lost, it redirects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!