ASP Classic和JavaScript检查会话状态

时间:2022-11-27 01:41:14

I am in need of some help with trying to figure out how to go about checking the users session and seeing if they are still logged in or not.

我需要一些帮助,试图弄清楚如何检查用户会话并查看他们是否仍然登录。

The problem being is because i have a static page that pretty much loads everything into "tabs" using jquery. So when the user navigates the page, it really never leaves that same page they started out on. Hints my problem in trying to determine if they are still logged in or not when they click on update their information on their profile or whatnot.

问题是因为我有一个静态页面,几乎使用jquery将所有内容加载到“选项卡”中。因此,当用户导航页面时,它实际上永远不会离开他们开始的相同页面。在他们点击更新他们的个人资料或诸如此类的信息时,试图确定他们是否仍然登录时提示我的问题。

3 个解决方案

#1


1  

Create some arbitary communication with the server when the user "navigates the page". For example when they click a "tab" despite it not needing from the server use AJAX to ping the server anyway.

当用户“浏览页面”时,与服务器建立一些任意通信。例如,当他们点击“标签”时,尽管它不需要从服务器使用AJAX来ping服务器。

Of course in reality you will want to check the session is live anyway after all the user could stare at the "Update" button from an hour before actually pressing it.

当然,实际上你会想要检查会话是否正常,因为所有用户都可以在实际按下它之前一小时盯着“更新”按钮。

You need both else you'll annoy users who have been busily using your app then find they're somehow timed-out anyway which will be contrary to their experiences on other sites.

你需要其他人,你会惹恼那些忙着使用你的应用程序的用户,然后发现他们无论如何都会超时,这将违背他们在其他网站上的体验。

In ASP you would store something in the Session object to indicate that the user is logged on. Here is an example "Am I logged on" page:-

在ASP中,您将在Session对象中存储一些内容以指示用户已登录。以下是“我登录后”页面的示例: -

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

Now you can use this page as your ping target. Ping just before update. If logged off I would suggest that you post details for update to the server any where but not to action the update. Request user loggon in your usual way with a redirect to a page that will then action those details.

现在您可以将此页面用作ping目标。在更新之前Ping。如果注销我会建议你发布更新到服务器的详细信息,但不要对更新进行操作。通过重定向到页面然后操作这些详细信息,以通常的方式请求用户loggon。

#2


1  

I would recommend that you take a slightly different approach.

我建议你采取稍微不同的方法。

Design your server side data-handling routines to return a special status, such as HTTP status 401 if the user is not logged in anymore.

设计服务器端数据处理例程以返回特殊状态,例如,如果用户不再登录,则返回HTTP状态401。

Then make your client side AJAX routines capable of handling this, i.e. by opening/showing your login screen, preferably without losing the data/context of the attempted operation.

然后让你的客户端AJAX例程能够处理这个,即打开/显示你的登录屏幕,最好不要丢失尝试操作的数据/上下文。

#3


0  

<TITLE>YourStaticPage.aspx</TITLE>
<%
  Session("username")="StealthRT"
  Session("email")="StealthRT@*users.com"
  Session("userid")=1
  Session("LastPageLoad")=System.DateTime.Now
%>

When you are handling the button_click event for your 'update profile' page you should be able to check if those variables are still stored in Session.Contents("username") or Session.Contents("LastPageLoad") etc.

当您处理“更新配置文件”页面的button_click事件时,您应该能够检查这些变量是否仍存储在Session.Contents(“username”)或Session.Contents(“LastPageLoad”)等中。

#1


1  

Create some arbitary communication with the server when the user "navigates the page". For example when they click a "tab" despite it not needing from the server use AJAX to ping the server anyway.

当用户“浏览页面”时,与服务器建立一些任意通信。例如,当他们点击“标签”时,尽管它不需要从服务器使用AJAX来ping服务器。

Of course in reality you will want to check the session is live anyway after all the user could stare at the "Update" button from an hour before actually pressing it.

当然,实际上你会想要检查会话是否正常,因为所有用户都可以在实际按下它之前一小时盯着“更新”按钮。

You need both else you'll annoy users who have been busily using your app then find they're somehow timed-out anyway which will be contrary to their experiences on other sites.

你需要其他人,你会惹恼那些忙着使用你的应用程序的用户,然后发现他们无论如何都会超时,这将违背他们在其他网站上的体验。

In ASP you would store something in the Session object to indicate that the user is logged on. Here is an example "Am I logged on" page:-

在ASP中,您将在Session对象中存储一些内容以指示用户已登录。以下是“我登录后”页面的示例: -

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

Now you can use this page as your ping target. Ping just before update. If logged off I would suggest that you post details for update to the server any where but not to action the update. Request user loggon in your usual way with a redirect to a page that will then action those details.

现在您可以将此页面用作ping目标。在更新之前Ping。如果注销我会建议你发布更新到服务器的详细信息,但不要对更新进行操作。通过重定向到页面然后操作这些详细信息,以通常的方式请求用户loggon。

#2


1  

I would recommend that you take a slightly different approach.

我建议你采取稍微不同的方法。

Design your server side data-handling routines to return a special status, such as HTTP status 401 if the user is not logged in anymore.

设计服务器端数据处理例程以返回特殊状态,例如,如果用户不再登录,则返回HTTP状态401。

Then make your client side AJAX routines capable of handling this, i.e. by opening/showing your login screen, preferably without losing the data/context of the attempted operation.

然后让你的客户端AJAX例程能够处理这个,即打开/显示你的登录屏幕,最好不要丢失尝试操作的数据/上下文。

#3


0  

<TITLE>YourStaticPage.aspx</TITLE>
<%
  Session("username")="StealthRT"
  Session("email")="StealthRT@*users.com"
  Session("userid")=1
  Session("LastPageLoad")=System.DateTime.Now
%>

When you are handling the button_click event for your 'update profile' page you should be able to check if those variables are still stored in Session.Contents("username") or Session.Contents("LastPageLoad") etc.

当您处理“更新配置文件”页面的button_click事件时,您应该能够检查这些变量是否仍存储在Session.Contents(“username”)或Session.Contents(“LastPageLoad”)等中。