asp.net - session 超时后重定向到登录页面

标签 asp.net .net vb.net visual-studio-2010 session-timeout

我发现了一些类似的问题,但没有一个给我真正需要的。

这是事情,我已将其添加到我的 web.config处理用户 session 过期:

<sessionState mode="InProc" timeout="1" />

1 分钟后,Session_End事件来自 Global.asax提出:
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("Login.aspx")
End Sub

这不起作用,因为:
Response is not available in this context.

(顺便说一句,this question 得到了一个回答,告诉我们这是可以的,并且得到了赞成)。

我不想要任何花哨的东西。我只想要一种简单的方法,可以在 session 时间到期时将用户重定向到登录页面。就这样。

谢谢你。

最佳答案

描述

您可以使用 Page_Init事件在 global.asax
样本

Protected Sub Page_Init(sender As Object, e As EventArgs)
 If Context.Session IsNot Nothing Then
  If Session.IsNewSession Then
   Dim newSessionIdCookie As HttpCookie = Request.Cookies("ASP.NET_SessionId")
   If newSessionIdCookie IsNot Nothing Then
    Dim newSessionIdCookieValue As String = newSessionIdCookie.Value
    If newSessionIdCookieValue <> String.Empty Then
     ' This means Session was timed Out and New Session was started
     Response.Redirect("Login.aspx")
    End If
   End If
  End If
 End If
End Sub

更多信息
  • Detecting Session Timeout And Redirect To Login Page In ASP.NET
  • 关于asp.net - session 超时后重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12502295/

    相关文章:

    .net - 如何保护用 C++ .NET 编写的应用程序

    javascript - 奇怪的编码输出,里面有 "\u0026 quot;"

    vb.net - Powershell 脚本可以在 Powershell 中运行,但不能在 VB.Net 中运行

    vb.net - 为什么 True 等于 -1

    vb.net - 我如何加粗列表框中的项目?

    javascript - JQuery 设置后找不到 .NET 隐藏字段值

    c# - 如何将 System.Windows.Media.Imaging.BitmapImage 转换为 System.Drawing.Image?

    asp.net - 将 Linq 转换为 SQL

    c# - ASP.NET 中 AjaxMin 5.14 和 Report Viewer 的问题

    c# - 从长摘要中获取前几个单词(纯字符串或 HTML)