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

后端 未结 4 1592
[愿得一人]
[愿得一人] 2021-01-21 03:53

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

相关标签:
4条回答
  • 2021-01-21 04:02

    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.

    0 讨论(0)
  • 2021-01-21 04:03

    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>
    
    0 讨论(0)
  • 2021-01-21 04:16

    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");
       }
    }
    
    0 讨论(0)
  • 2021-01-21 04:21

    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.

    0 讨论(0)
提交回复
热议问题