how to redirect user to default page on session time out in asp.net 3.5

爱⌒轻易说出口 提交于 2020-01-11 09:59:27

问题


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.


回答1:


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.




回答2:


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>



回答3:


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



回答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.



来源:https://stackoverflow.com/questions/22803858/how-to-redirect-user-to-default-page-on-session-time-out-in-asp-net-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!