• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1886
  • Last Modified:

Redirect to User Login Page When Session Expires

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?
  • 4
  • 3
  • 3
2 Solutions
What scrripting language are you using?
In asp you can do something like this instead of normal response.redirect

    url = "http://www.yourdomain.com/redirectpage.asp
    response.write("<script>" & vbCrLf)
    response.write("parent.location.href='" & url & "';")
    response.write(vbCrLf & "</script>")


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.
scissorhandAuthor Commented:
I am using ASP .NET
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

scissorhandAuthor Commented:
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
c# redirect code :

string strRedirect;
strRedirect = "<script language='Javascript'>";
strRedirect += "parent.location.href=yourloginPage.aspx';";
strRedirect += "</script>";
scissorhandAuthor Commented:
This is the code to redirect the page, which I know about it. But how to link it with session expire?
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.
if(Session("value") == "")
 --- above code goes here.....
Split between Vinod_nk and me perhaps? Vinod_nk tells how to achieve the redirect and my answer tells when to do it.
Strange split between Vinod_nk & Vinod_nk?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now