• 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?
0
scissorhand
Asked:
scissorhand
  • 4
  • 3
  • 3
2 Solutions
 
Vinod_nkCommented:
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>")

%>

Rgds
V
0
 
TheMegaLoserCommented:
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" />
                    </credentials>
               </forms>
          </authentication>
--- 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">
          <system.web>
               <authorization>
                    <allow users="user" />
                    <deny users="*" />
               </authorization>
          </system.web>
     </location>
--- 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)
        Else
            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
System.Web.Security.FormsAuthentication.SignOut()
--- logoff.aspx bit end

Now you just give your users the link to the protected page and they will be prompted to log in.
0
 
scissorhandAuthor Commented:
I am using ASP .NET
0
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:
TheMegaLoser,
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
0
 
Vinod_nkCommented:
c# redirect code :

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