Hi, in my asp.net application i need to redirect the user to a new page when the session is timedout. I am using windows authentication for the web pages. i can not handle that in session_end event.

Any help in this regard will be very much helpful. Thanks a lot.

Rathi

Dani AI

Generated

A reliable pattern for this scenario is to detect the expired session on the next HTTP request and redirect the user to the Hub page there. Session_End cannot perform a redirect (it runs outside an active HTTP request and only fires for InProc session mode), so it is not suitable for sending a client to another page. 's forms-auth sample is a useful reference for login flows, but with Windows authentication the OS/IIS keeps the user authenticated — the app still needs to detect missing session data and act accordingly.

A practical server-side option is a single check that every page (or a BasePage/MasterPage) runs: if a chosen session marker (for example Session["AppInitialized"]) is missing and the request is for an .aspx page that is not the Hub, redirect to the Hub. Example BasePage check:

public class BasePage : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        string path = Request.AppRelativeCurrentExecutionFilePath;
        if (!path.Equals("~/Hub.aspx", StringComparison.OrdinalIgnoreCase)
            && Session["AppInitialized"] == null)
        {
            Response.Redirect("~/Hub.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}

If a client-side experience is preferred, inject a small script using Session.Timeout to schedule a redirect, but reset that timer on each server response (or use an activity listener or heartbeat AJAX ping) so normal activity does not cause a false redirect. Always exclude the Hub page from the redirect check and be careful with AJAX requests and static resources to avoid unwanted redirects or loops.

Recommended Answers

All 2 Replies

hi you can read my article on implementing simple login system using asp.net. You can also download the source for the sample app here.

Hi Thanks for the reply, but i am not using the forms authentication and its a windows authentication and i have a main page like "Hub" pahe which has links to all other pages, and i want the user to be redirected to the hub page when the session is timed out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.