Advertisement

05.13.2008 at 01:27PM PDT, ID: 23399321 | Points: 250
[x]
Attachment Details

Redirect to Login when Session Expires (asp 2.0 Form Authentication)

Asked by gigglick in WebApplications, Miscellaneous Web Development

Hi -

I've tried a whole bunch of suggestions I found on msdn to get my site to redirect to a login page when my session expires but I haven't had any luck.  Does anyone know a standard way to so this that is consistent.
My webconfig settings are:

            <authentication mode="Forms">
                  <forms loginUrl="~/Login/Login.aspx" name="adAuthCookie" timeout="1" path="/">
                  </forms>

            </authentication>
            <authorization>
                  <deny users="?"/>
                  <allow users="*"/>
            </authorization>

Global.asax

    void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        if (null == authCookie)
        {
            //There is no authentication cookie.
            return;
        }
        FormsAuthenticationTicket authTicket = null;
        try
        {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        }
        catch (Exception ex)
        {
            //Write the exception to the Event Log.
            return;
        }
        if (null == authTicket)
        {
            //Cookie failed to decrypt.
            return;
        }
        //When the ticket was created, the UserData property was assigned a
        //pipe-delimited string of group names.
        string[] groups = authTicket.UserData.Split(new char[] { '|' });
        //Create an Identity.
        GenericIdentity id = new GenericIdentity(authTicket.Name, "LdapAuthentication");
        //This principal flows throughout the request.
        GenericPrincipal principal = new GenericPrincipal(id, groups);
        Context.User = principal;
    }
    protected void Session_End(Object sender, EventArgs e)
    {
        //having problem with this -- page is not being re-directed when the session expires
        //need to check session state on each page?
        Response.Redirect("Login.aspx");
    }
Start Free Trial
 
 
[+][-]05.13.2008 at 02:09PM PDT, ID: 21559316

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.11.2008 at 07:58AM PDT, ID: 21760709

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628