Click here to Skip to main content
16,020,974 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please correct me...so basically i had implemented one code where i have used session variable now on successful login user can access through pages but aftr session expired it should again redirect to login page and at same time should prevent direct url access..how to do this?
Posted

On every page load, check if session is null or not and do the appropriate action.
Something like,
C#
protected void somepage_load(... ...)
{
    if(Session["UserSession"] == null)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        // load the page
    }
}


-KR
 
Share this answer
 
Use Global .asax file to check session.

Below link will be useful to you Try this
http://www.c-sharpcorner.com/UploadFile/0c1bb2/redirect-page-after-session-time-out-in-Asp-Net424/[^]
 
Share this answer
 
Comments
Member 11932995 23-Oct-15 4:04am    
thanks ,i followed your suggestions but now it is showing This webpage has a redirect loop error...and also this is not even restricting direct url access ...please help
this is my code,
if (!IsPostBack)
{
if (Session["DNA"] != null)
{
Response.Redirect("loginpage.aspx");

}
else
if(!HttpContext.Current.Request.Path.EndsWith("aboutus.aspx", StringComparison.InvariantCultureIgnoreCase))
Response.Redirect("aboutus.aspx");


}
so here this code is restricting direct url access but on logged in successfully while accessing directly it should redirect to about us page but instead it is redirecting and be in aloop of showing index page as the login page code is:
protected void btn_login_Click(object sender, EventArgs e)
{
if (Session["DNA"] != null)
{
string wrng_uname = "Enter the correct Username !";
string wrng_pswrd = "You Have Entered WRONG password !";

if (txt_uname.Text != "DNA")
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + wrng_uname + "');", true);
}
else
if (txt_uname.Text == "DNA" && txt_password.Text != "qwaszx123")

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + wrng_pswrd + "');", true);

else
if (txt_uname.Text == "DNA" && txt_password.Text == "qwaszx123")
{
Response.Redirect("index.aspx");

}
}

}
Sagar Haridas Shinde 23-Oct-15 6:27am    
you are redirecting to same page again and again, so it created a loop

make sure that only redirected to a different page

if(!HttpContext.Current.Request.Path.EndsWith("PageName.aspx", StringComparison.InvariantCultureIgnoreCase))
Response.Redirect("PageName.aspx");

or


Try adding this to your web.config file:




http://stackoverflow.com/questions/15894254/how-to-solve-redirect-loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900