使用 javascript 检测会话是否过期

发布于 2024-09-11 02:48:29 字数 488 浏览 4 评论 0原文


我正在使用 ASP.Net 创建一个 Web 应用程序。我在我的应用程序中使用会话。
在一种情况下,我尝试通过 Javascript 访问会话变量。当我在正常状态下访问它时,它工作正常。但会话过期后,如果点击任何按钮,都会抛出异常,行为异常,导致页面找不到。
这是我正在使用的代码;

 var TransactionID = '<% =((Hashtable)(Session["SessionData"]))["TransactionID "] %>';
 if(TransactionID !='')
 {
    //action
 }

我正在尝试在 C# 中实现类似的目标;

String lsStr;
if(null != lsStr)
{
    //action
}

如果有人知道如何使用 javascript 检查会话是否存在,请帮助我。
提前致谢。

I am creating a web application using ASP.Net. I am using session in my application.

There is one scenario where I am trying to access session variable through Javascript. It is working fine, when I m accessing it in normal state. But after session expiry, if click on any button, it throws exception and behaves abnormally, resulting in page not found.

Here is the code I am using;

 var TransactionID = '<% =((Hashtable)(Session["SessionData"]))["TransactionID "] %>';
 if(TransactionID !='')
 {
    //action
 }

I am trying to achieve something like of sort in c#;

String lsStr;
if(null != lsStr)
{
    //action
}

Please help me, if anyone knows how to check if session exists or not using javascript.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-09-18 02:48:29

我会在您的代码隐藏页面中推荐一个公共属性,它可以:

public string TransactionID
{
   get { 
        var data = ((Hashtable)(Session["SessionData"])); 
        if (data != null)
           return data["TransactionID "];
        else
           return null;
   }
}

避免立即出现的错误。如果想要一个轮询机制知道什么时候过期,可以考虑JS定时器倒计时/AJAX调用服务器的思路。如果不向服务器请求信息,则无法从客户端检查会话。

但是,使用上面的属性,当页面回发时,客户端上的事务 ID JS 变量将为 null,这也是识别 Session 过期的一种方法。

HTH。

I would recommend a public property in your code-behind page that does:

public string TransactionID
{
   get { 
        var data = ((Hashtable)(Session["SessionData"])); 
        if (data != null)
           return data["TransactionID "];
        else
           return null;
   }
}

To get around the immediate error. If you want a polling mechanism to know when it expires, consider the JS timer countdown/AJAX call to the server idea. There is no way to check session from the client without requesting information from the server.

But, using the property above, when the page posts back, and on the client the transaction ID JS variable would be null, that would be a way of identifying that the Session expired too.

HTH.

标点 2024-09-18 02:48:29

建议将此检查放在计时器客户端中,每次运行时都会延长会话,导致会话永远不会过期,仅当这是所需的功能时才执行此操作。

最好的办法是仅在需要时执行检查,方法是向不需要身份验证的页面发出 ajax 请求,然后该页面可以测试某个会话变量是否存在并返回 true 或 false。

举个例子,我最近开发的一个应用程序有一个带有客户端逻辑的页面,需要进行检查以确保会话仍然存在。在本例中,对于该页面,我知道 userID 始终存储在 Session["UserID"] 中,因此我创建了 c# ajax 页面,如果该对象为 null,则返回 false;如果存在,则返回 true,这允许我从 JavaScript 调用该页面并告知会话是否已过期。这可能需要进行一些更改以适应您的情况,但我确信您可以使用相同的逻辑做一些事情。

The suggestion to put this check in a timer client side would extend the session every time it ran causing the session never to expire, Only do this if this is the desired functionality.

The best bet is to perform the check only when you need to by doing an ajax request to a page that doesn't require authentication that page could then test if a certain session variable exists and return true or false.

As an example an application I recently worked on had a page with client side logic that required checks to make sure the session still existed. In this case for that page I knew the userID was always stored in Session["UserID"] so I created a c# ajax page that just returned false if this object was null and true if it existed this allowed me to calls this page from JavaScript and tell if the session had expired. This will probably need to be changed a bit to fit your situation but I'm sure you could do something using the same logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文