繁体   English   中英

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

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

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

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

这种技术是否适用于.net 3.5框架应用程序。

对于无主页面:

你可以试试这个。

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

        }
    }
    else
    {
        Response.Redirect("Default.aspx", false);
    }
}

在每个网页中使用此逻辑

如果使用母版页:

在masterpage.cs文件中使用上述逻辑

使用Web.Config:

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

您可以在page_init上查看会话,如下所示

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

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

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

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

我会为除SignIn.aspx之外的所有webforms使用一个SignIn.aspx ,并在masterpages init方法中使用它:

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

关于表单身份验证的MSDN文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM