C# 如何在asp.net 3.5中将用户重定向到会话超时的默认页面

C# 如何在asp.net 3.5中将用户重定向到会话超时的默认页面,c#,asp.net,.net,session-state,session-timeout,C#,Asp.net,.net,Session State,Session Timeout,我只想在asp.net 3.5中会话过期时将用户重定向到主页(Default.aspx)。 我只是通过网络用户控制来实现的,但它并不是完美的。所以我只想用web.config来做 <authentication mode="Forms"> <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" /> </authentication> 此技术是否适用于.net

我只想在asp.net 3.5中会话过期时将用户重定向到主页(Default.aspx)。 我只是通过网络用户控制来实现的,但它并不是完美的。所以我只想用web.config来做

<authentication mode="Forms">
  <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" />
</authentication>


此技术是否适用于.net 3.5 framework应用程序。

您可以在页面上检查会话,如下所示

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

除了
SignIn.aspx
之外,我会对所有webforms使用母版页,并在母版页初始化方法中使用该母版页:

if((System.Web.HttpContext.Current.User == null) || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    Response.Redirect("SignIn.aspx");

无母版页:

<authentication mode="Forms">
  <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
你可以试试这个

protected void Page_Load(object sender, EventArgs e)
  {
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (!IsPostBack)
        {

        }
    }
    else
    {
        Response.Redirect("Default.aspx", false);
    }
}
在每个网页中使用此逻辑

如果使用母版页:

<authentication mode="Forms">
  <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
在masterpage.cs文件中使用上述逻辑

使用Web.Config:

<authentication mode="Forms">
  <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" />
</authentication>
<authorization>
<deny users="?" />
</authorization>

如果您使用的是表单身份验证,则无需编写任何自定义代码。For会话超时设置由框架本身提供。只需更改配置文件,如下所述:

<authentication mode="Forms">
    <forms defaultUrl="~/Default.aspx"
        loginUrl="~/Login.aspx"
        slidingExpiration="true"
        timeout="60" />
</authentication>


当会话过期时,上述配置将用户重定向到登录页面

为什么每个网页都有asp.net webforms的母版页?我可以将默认url和登录url设置为相同吗。