Redirect to User Login Page When Session Expires

Posted on 2006-03-19
Last Modified: 2007-11-27
I have a main page with frames, meaning there are a few sub pages. How do I redirect the page to the login page which is only a single page when any session in the sub pages expire?
Question by:scissorhand
    LVL 8

    Assisted Solution

    What scrripting language are you using?
    In asp you can do something like this instead of normal response.redirect

        url = ""
        response.write("<script>" & vbCrLf)
        response.write("parent.location.href='" & url & "';")
        response.write(vbCrLf & "</script>")


    LVL 12

    Expert Comment

    If you're using ASP .NET you can do like this:

    Use the Forms authentication in .NET.

    First of all, modify your web.config according to the following:

    --- web.config bit start
              <authentication mode="Forms">
                   <forms loginUrl="login.aspx">
                        <credentials passwordFormat="Clear">
                             <user name="user" password="pass" />
    --- web.config bit end

    This creates a user with the user name user and the password pass. It also sets the login page to login.aspx.

    Next, determine which pages should be protected. This bit is also placed in web.config outside of the system.web tag.

    --- web.config bit start
         <location path="MyProtectedDirectory">
                        <allow users="user" />
                        <deny users="*" />
    --- web.config bit end

    Now only user is allowed to see MyProtectedDirectory.

    Then create a standard login page. When the user presses the login button you run the following code:

    --- login.aspx bit start
            If FormsAuthentication.Authenticate(txtUser.Text, txtPwd.Text) Then
                FormsAuthentication.RedirectFromLoginPage(txtUser.Text, False)
                lblMessage.Text = "Invalid username"
            End If
    --- login.aspx bit end

    txtUser and txtPwd is of course your textboxes and lblMessage is a label for displaying a message back to the user.

    As for question nr 2, just run this code when the user presses the logoff button:

    --- logoff.aspx bit start
    --- logoff.aspx bit end

    Now you just give your users the link to the protected page and they will be prompted to log in.

    Author Comment

    I am using ASP .NET

    Author Comment

    U misunderstand my question already, I want the page to be redirected to login page when the session expires, not for authentication, the page contains a few frames, but the login page is just a single page, I want to know the setting needed and the codes
    LVL 8

    Accepted Solution

    c# redirect code :

    string strRedirect;
    strRedirect = "<script language='Javascript'>";
    strRedirect += "parent.location.href=yourloginPage.aspx';";
    strRedirect += "</script>";

    Author Comment

    This is the code to redirect the page, which I know about it. But how to link it with session expire?
    LVL 12

    Expert Comment

    In Global.asax.cs you have a function:

    protected void Session_End(Object sender, EventArgs e)

    This method is not supported if you use StateServer or SQLServer Session mode.
    LVL 8

    Expert Comment

    if(Session("value") == "")
     --- above code goes here.....
    LVL 12

    Expert Comment

    Split between Vinod_nk and me perhaps? Vinod_nk tells how to achieve the redirect and my answer tells when to do it.
    LVL 12

    Expert Comment

    Strange split between Vinod_nk & Vinod_nk?

    Featured Post

    Training Course: Adobe Dreamweaver CC 2015

    Adobe Dreamweaver Creative Cloud is used by web designers and front-end developers and allows you to visualize your site in real-time as you code. This course covers exam objectives for the Adobe Certified Associate (ACA) certification.

    Join & Write a Comment

    Building “do-it-yourself” web sites has become an epidemic. There are so many blogs, web sites and even books that "teach" you how to build your web site in a few extremely simple and easy steps. Building a web site has become easier than boiling an…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to dynamically set the form action using jQuery.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now