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
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.
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>
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");
}
}
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.