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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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>")


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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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>";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.