gpt4 book ai didi

session - 使用 ASP.NET 2.0 处理过期 session 的最有效方法是什么

转载 作者:行者123 更新时间:2023-12-01 10:16:48 29 4
gpt4 key购买 nike

在我们正在 build 的网站上。我们需要能够在用户 session 结束时将用户重定向到默认页面。

乍一看,我们使用带有 Response.Redirect 的 Session_End 来完成这项工作。

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("~/global/exit.aspx")
End Sub

但它会生成一堆 Response is not available in this context 错误。我们自然不想向我们的服务器错误日志发送垃圾邮件。

处理以 ASP.NET 2.0 结束的 session 的最有效方法是什么?

最佳答案

我们在 global.asax.cs 文件中添加了以下代码:

 private void IsAuthenticated()
{
string vFileName = Path.GetFileName(HttpContext.Current.Request.Path);
string vExt = Path.GetExtension(vFileName).ToLower();
if ((vFileName != "Login.aspx") && (vExt == ".aspx"))
{
if (HttpContext.Current.Session["LoggedIn"] == null)
{
HttpContext.Current.Response.Redirect("~/Login.aspx");
}
}
}
void Application_PostAcquireRequestState(object sender, EventArgs e)
{
IsAuthenticated();
}

NS:我们的 Global .asax 文件中的第一行是:

<%@ Application  Inherits="???.Global" Language="C#" %>

关于session - 使用 ASP.NET 2.0 处理过期 session 的最有效方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/201370/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com