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

We are building on the site. We should be able to redirect the user to the default page when his session ends.

At first glance, we used Session_End with Response.Redirect to complete this task.

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("~/global/exit.aspx")
End Sub

But it generates crapload from Response, which is not available in this context. Naturally, we do not want to spam our server error logs.

What is the most efficient way to handle a session ending in ASP.NET 2.0?

+3
source share
3 answers

We have 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: first line in our global .asax file:

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

, , Application.Begin_Request , , .

+2

You can use the session_end method because it is not a user-invoked method, it is started by ASP.NET and the response is not available, since it is not part of the request.

The biggest way is to check and see if there is a session somewhere in the loading of your pages and redirect back to the main one.

What I did earlier is to set this control logic on the RestrictedPage.master main page, which was used for all pages related to the session, if the session is lost, it redirects.

+2
source

Source: https://habr.com/ru/post/1698253/


All Articles