i just want to redirect user to Home Page(Default.aspx) when session has been expired in asp.net 3.5.
i just do it with web user control but steel it’s not work perfectly. so i just want to do it with web.config.
<authentication mode="Forms"> <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" /> </authentication>
Is this technique works in .net 3.5 framework application.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For No Master Page:
you may try this.
protected void Page_Load(object sender, EventArgs e)
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
if (!IsPostBack)
{
}
}
else
{
Response.Redirect("Default.aspx", false);
}
}
Use this logic in every webpage
If Master Page is Used:
Use the above logic in your masterpage.cs file
Using Web.Config:
<authentication mode="Forms"> <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" /> </authentication> <authorization> <deny users="?" /> </authorization>
Method 2
You can check for session on page_init as show below
protected void Page_Init(object sender, EventArgs e)
{
CheckSession();
}
private void CheckSession()
{
if (Session["SessionID"] == null || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect("Default.aspx");
}
}
Method 3
If you are using form authentication then you don’t have to write any custom code. For session timeout settings are provided by Framework itself. Just change the configuration file as mentioned below :
<authentication mode="Forms">
<forms defaultUrl="~/Default.aspx"
loginUrl="~/Login.aspx"
slidingExpiration="true"
timeout="60" />
</authentication>
Above configuration will redirect user to the login page when session expires.
Method 4
I would use a masterpage for all webforms except the SignIn.aspx and have this in the masterpages init method:
if((System.Web.HttpContext.Current.User == null) || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
Response.Redirect("SignIn.aspx");
MSDN article about forms authentication.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0