简体   繁体   English

经典 ASP / 会话结束重定向

[英]Classic ASP / End of session redirect

I want to automatically redirect to the login page when the users session has expired.我想在用户会话过期时自动重定向到登录页面。

I have been using the following code in an include file that sits at the top of every page in my application:我一直在位于应用程序每个页面顶部的包含文件中使用以下代码:

Session.Timeout = 60
Response.AddHeader "Refresh", CStr(CInt(Session.Timeout + 1) * 60)
Response.AddHeader "cache-control", "private"
Response.AddHeader "Pragma","No-Cache"
Response.Buffer = True
Response.Expires = 0
Response.ExpiresAbsolute = 0

If Session("accountID") = "" Then
    Response.Redirect("http://www.mydomain.com/")
End If

This works but there is very slight bug.这有效,但有非常轻微的错误。 Every now and then the page will refresh even though the session is still alive and it seems that it refreshes before the 60 minutes is up!即使会话仍然存在,页面也会时不时地刷新,并且似乎在 60 分钟结束之前刷新!

Can anybody see what the problem is or can you suggest a different method?任何人都可以看到问题所在,或者您可以提出不同的方法吗?

Seeing as though you have to do this client side I'd favour JavaScript/jQuery and AJAX over that method.好像你必须做这个客户端,我更喜欢 JavaScript/jQuery 和 AJAX。 Here's an example of how to do it.这是如何做到这一点的示例。

Essentially you just set-up an AJAX call to poll a script which returns (in JSON format) whether the user is logged in or not;本质上,您只需设置一个 AJAX 调用来轮询一个脚本,该脚本返回(以 JSON 格式)用户是否登录; if they're not then you can transfer them to another page.如果不是,那么您可以将它们转移到另一个页面。

The benefits to this method are that you can poll whenever you want;这种方法的好处是你可以随时轮询; eg every 10 seconds to see whether the user is still logged in rather than having to wait a full hour.例如,每 10 秒查看用户是否仍在登录,而不必等待整整一个小时。 It also means that you don't need to state the session time-out figure in your code and so you can leave that to be determined in IIS.这也意味着您不需要在代码中说明会话超时数字,因此您可以将其留在 IIS 中确定。 Also if the user logged off elsewhere in your system, or your application pool recycled and their session was reset this would detect it fairly quickly.此外,如果用户在您系统的其他地方注销,或者您的应用程序池被回收并且他们的会话被重置,这将很快检测到它。

I notice from your profile that you're a Paparazzi photographer.我从你的个人资料中注意到你是一名狗仔队摄影师。 I'd consider this the DSLR method and the response header method the cheap phone camera method :o.我认为这是 DSLR 方法和响应头方法便宜的手机摄像头方法:o。

To build your session checker page create a file called session.asp (in the same folder as your other files to make life simpler).要构建您的会话检查器页面,请创建一个名为 session.asp 的文件(与您的其他文件位于同一文件夹中,以简化操作)。 In it put:里面写着:

<%
Response.ContentType = "application/json"
If Session("LoggedOn") Then
   Response.Write "{""loggedOn"": true}"
Else
   Response.Write "{""loggedOn"": false}"
End If
%>

If the user is logged in it returns {"loggedOn": true}, if they're not {"loggedOn": false}.如果用户已登录,则返回 {"loggedOn": true},如果不是 {"loggedOn": false}。 This is what we'll use on your other page to poll if they're logged in by calling this page periodically and reading the response.这是我们将在您的其他页面上通过定期调用此页面并阅读响应来轮询他们是否已登录的内容。

Now onto your pages which originally had your Response.AddHeader code in. Remove all of your code as this replaces it.现在到您的页面上,这些页面最初包含您的 Response.AddHeader 代码。删除所有代码,因为这将替换它。

First make sure you have a reference to jQuery on your pages:首先确保您的页面上有对 jQuery 的引用:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

And then put under this line the following:然后在这一行下面写上以下内容:

<script type="text/javascript">
    $(document).ready(function() {

        var checkLoggedOn = function() {
            $.getJSON('session.asp', function(data) {
                if (!data.loggedOn)
                    window.location.replace("http://stackoverflow.com");
            });
        };

        // Call checkLoggedOn every x milliseconds
        setInterval(checkLoggedOn, 30000);
    });
</script>

All being well, it should work.一切都很好,它应该工作。 I set the above to poll every 30 seconds (30000) but you could increase/decrease this to whatever you wanted.我将上面的设置设置为每 30 秒(30000)轮询一次,但您可以将其增加/减少到您想要的任何值。

Note I borrowed large parts of the code above from https://stackoverflow.com/a/4928564/171703 and https://stackoverflow.com/a/2709160/171703 .注意我从https://stackoverflow.com/a/4928564/171703https://stackoverflow.com/a/2709160/171703借用了上面的大部分代码。


Update:更新:

From the comments below, if you want the user's session to expire after the timeout figure (whether they are keeping their session alive or not) then you could do this.从下面的评论中,如果您希望用户的会话在超时数字之后过期(无论他们是否保持会话活动),那么您可以这样做。

When the user is logged in, set a new session variable for LoginExpiration:当用户登录时,为 LoginExpiration 设置一个新的会话变量:

Session("LoginExpiration") = DateAdd("n", Session.TimeOut, Now())

This takes the current time and adds to it the session timeout figure - giving you the time when their session should be destroyed.这需要当前时间并将会话超时数字添​​加到其中 - 为您提供他们的会话应该被销毁的时间。

If you now modify your session.asp to the following it takes the LoginExpiration figure and returns that the user is not logged in the event of:如果您现在将 session.asp 修改为以下内容,它将获取 LoginExpiration 数字并在出现以下情况时返回用户未登录:

  1. The users session has timed out (IIS application pool reset, or they clicked logoff etc)用户会话已超时(IIS 应用程序池重置,或者他们单击了注销等)
  2. The current date/time is greater than the set LoginExpiration time当前日期/时间大于设置的 LoginExpiration 时间

Which is:这是:

<%
Response.ContentType = "application/json"

LoggedOn = "false"
LoginExpiration = Session("LoginExpiration")
DateNow = Now()

If IsDate(LoginExpiration) Then
    If DateNow < LoginExpiration Then
        LoggedOn = "true"
    End If
End If

Response.Write "{"
    Response.Write """loggedOn"": " & LoggedOn & ", "
    Response.Write """loginExpiration"": """ & LoginExpiration & """"
Response.Write "}"
%>

I've put the loginExpiration figure into the JSON response so you could work with it client side if you wanted too.我已将 loginExpiration 数字放入 JSON 响应中,因此您也可以在客户端使用它。

The ASP Global.asa file may be what you're looking for. ASP Global.asa文件可能就是您要查找的文件。 It allows you to run code at certain events, such as Session start and end.它允许您在某些事件中运行代码,例如会话开始和结束。 See https://www.w3schools.com/asp/asp_globalasa.asp for more info, but I believe the following will work:有关更多信息,请参阅https://www.w3schools.com/asp/asp_globalasa.asp ,但我相信以下方法会起作用:

Global.asa环球网

<script language="vbscript" runat="server">
    sub Session_OnEnd
        Response.Redirect("http://www.example.com/")
    end sub
</script>
'If the session variable is False or does not exist (IsNull)
'then redirect the user to the unauthorised user page
If Session("accountID") = False or IsNull(Session("accountID")) = True then
 'Redirect to unathorised user page
 Response.Redirect "pagename.asp"
End If 

Place this in an include file that you include in all pages you need protected.将它放在一个包含文件中,该文件包含在您需要保护的所有页面中。

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="checkmem.asp"-->
<!--#include file="includes/dtdsql.asp" -->
<!--#include file="includes/functions.asp" -->

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

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