I am working on a ASP.NET website. I am redirecting the users to logout page if the session has expired. In the page load of Logout page, I am clearing all the session variables.
I want to redirect the users to login page from logout page on click on a link. I am checking Request.Params["__EVENTTARGET"] in page load to see if the post back is caused by link button click. but it always return null
value. Link button click event does not fire. How to redirect users to login page from Logout page after the session has expired or cleared?
Why are you logging users out if their session has expired? If your authentication and your session are somehow linked then you're better re-coding your site to break the dependence between the two.
However if you want to detect that the current session is a new session you can use the code in this article
Based on your post, I made a test on computer, it worked fine. You may refer to below code.
Each page(except Login Page) add this function at Page_Load event
if (Session["UesrID"] == null)
{
Session.Remove();
Response.Redirect("LoginPage.aspx");
string NowPage="the name of the Page";//the page of login out,then you want to back it
Session["NowPage"] = NowPage;
}
Add this function to Login Page
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)//Pay attention to this, it is IsPostBack,also you could revise it in your need
{
if (Session["UesrID"] == null)
{
Session.Remove();
Response.Redirect("LoginPage.aspx");
string NowPage="the name of the Page";//the page of login out,then you want to back it
Session["NowPage"] = NowPage;
}
else
{
if(/*sign in successfully*/)
{
UserID = Session["UserID"];
string BackPage="";
BackPage = Session["NowPage"].ToString();
if(BackPage=="LoginPage.aspx")
{
Response.Redirect("UserPage");
}
else
{
Response.Redirect(BackPage);
}
}
else
{
//sign in unsuccessfully
}
}
}
}
Hope this could be helpful to you.
Best regards,
Archer Wang
We are trying to better understand customer views on social support experience. Click HERE to participate the survey. Thanks!
Member
89 Points
156 Posts
ASP.NET website - Re direct logout page if session has expired and link to Login again
Nov 24, 2014 06:32 AM|ashok.k|LINK
<div link="#0563C1" vlink="#954F72" lang="EN-US"> <div class="x_WordSection1">
Hi ,
I am working on a ASP.NET website. I am redirecting the users to logout page if the session has expired. In the page load of Logout page, I am clearing all the session variables.
I want to redirect the users to login page from logout page on click on a link. I am checking Request.Params["__EVENTTARGET"] in page load to see if the post back is caused by link button click. but it always return null value. Link button click event does not fire. How to redirect users to login page from Logout page after the session has expired or cleared?
Logout.cs file:
//page load
string str = Request.Params["__EVENTTARGET"]; //returns always null
if (!IsPostBack)
{
Session.Clear();
Session.RemoveAll();
Session.Abandon();
}
// button click
protected void lnkLogin_Click(object sender, EventArgs e)
{
try
{
Response.Redirect("Login.aspx",true);
}
catch (Exception ex)
{
}
}
--Web config file
<authentication mode="Forms">
<forms loginUrl="~/Logout.aspx" timeout="2880" slidingExpiration="true"/>
</authentication>
Request.Params["__EVENTTARGET"] is always null in the page load of Logout page.
How to redirect users to login page from Logout page after the session has expired or cleared?
Thanks
Ashok
</div> </div>
All-Star
37441 Points
9076 Posts
Re: ASP.NET website - Re direct logout page if session has expired and link to Login again
Nov 24, 2014 07:10 AM|AidyF|LINK
Why are you logging users out if their session has expired? If your authentication and your session are somehow linked then you're better re-coding your site to break the dependence between the two.
However if you want to detect that the current session is a new session you can use the code in this article
http://aspalliance.com/520_detecting_aspnet_session_timeouts.2
I found that article by googling "asp.net detect new session"
Contributor
5642 Points
944 Posts
Re: ASP.NET website - Re direct logout page if session has expired and link to Login again
Nov 25, 2014 04:26 AM|Archer Wang|LINK
Hi ashok.k,
Based on your post, I made a test on computer, it worked fine. You may refer to below code.
Each page(except Login Page) add this function at Page_Load event
Add this function to Login Page
Hope this could be helpful to you.
Best regards,
Archer Wang