Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1879
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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