【问题标题】:how to redirect user to default page on session time out in asp.net 3.5如何在asp.net 3.5中的会话超时时将用户重定向到默认页面
【发布时间】:2014-05-13 06:54:44
【问题描述】:
当会话在 asp.net 3.5 中过期时,我只想将用户重定向到主页(Default.aspx)。
我只是用网络用户控制来做,但是钢它不能完美地工作。所以我只想用 web.config 来做。
<authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" />
</authentication>
这种技术是否适用于 .net 3.5 框架应用程序。
【问题讨论】:
标签:
c#
asp.net
.net
session-state
session-timeout
【解决方案1】:
对于无母版页:
你可以试试这个。
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>
【解决方案2】:
您可以在 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");
}
}
【解决方案3】:
如果您使用表单身份验证,那么您不必编写任何自定义代码。会话超时设置由框架本身提供。只需按如下所述更改配置文件:
<authentication mode="Forms">
<forms defaultUrl="~/Default.aspx"
loginUrl="~/Login.aspx"
slidingExpiration="true"
timeout="60" />
</authentication>
以上配置将在会话到期时将用户重定向到登录页面。
【解决方案4】:
我会为除SignIn.aspx 之外的所有网络表单使用母版页,并将其包含在母版页的 init 方法中:
if((System.Web.HttpContext.Current.User == null) || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
Response.Redirect("SignIn.aspx");
MSDN article about forms authentication.