当用户离开站点时,如何强制 ASP.Net 会话过期?

发布于 2024-07-15 23:30:03 字数 598 浏览 5 评论 0原文

我们有一个场景,我们希望检测用户何时离开我们的站点并立即终止其 .Net 会话。 我们正在使用表单身份验证。 我们不是在谈论会话超时,我们已经有了会话超时。 我们想知道用户何时通过链接、输入地址或跟随书签浏览离开我们的网站。 如果他们返回我们的网站,即使是立即,他们也必须重新登录(我知道这不是很好的可用性 - 这是我们的客户给我们的安全要求)。

我最初的直觉是,这要么不可能,要么任何解决方案都极其不可靠。 我们提出的唯一解决方案是:

  • 添加一个 JavaScript onBlur 事件处理程序,告诉服务器在用户离开站点时注销会话。
  • 用户登录后,检查 HTTP 引荐来源网址以确保用户已从站点内导航。
  • 将 AJAX 轮询添加回服务器以保持会话刷新(可能每隔 10 秒一次)。 当未按时接到电话时,会话将结束。

onBlur 似乎是最简单但可能最不可靠的方法 - 我不确定它是否有效。 引用方法也存在问题,因为用户可以在网站内输入地址而不点击链接。 AJAX 方法似乎可以工作,但它很复杂 - 我什至不知道如何在后端处理它。 我认为在某些情况下这可能并不总是有效。

任何想法,将不胜感激。 谢谢。

We have a scenario in which we like to detect when the user has left our site and immediately expire their .Net session. We're using Forms Authentication. We're not talking about a session timeout, which we already have. We would like to know when a user has browsed away from our site, either via a link, by typing in an address or following a bookmark. If they return to our site, even if right away, they will have to log back in (I understand this is not great usability - this is a security requirement we've been given by our client).

My initial instinct is that this is either not possible, or that any solutions will be extremely unreliable. The only solutions we've come up with are:

  • Add a JavaScript onBlur event handler that tells the server to log out the session when the user leaves the site.
  • Once the user has logged in, check the HTTP referrer to ensure that the user has navigated from within the site.
  • Add AJAX polling back to the server to keep the session refreshed, possibly on a 10-second interval. When the call isn't received on time the session would end.

The onBlur seems like the easiest, but possibly least reliable method - I'm not sure if it would even work. There are also issues with the referrer method, as the user could type in an address within the site and not follow a link. The AJAX method seems like it would work, but it's complicated - I'm not even sure how to handle it on the back-end. I'm thinking there might also be scenarios in which that wouldn't always work.

Any ideas would be appreciated. Thanks.

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

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

发布评论

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

评论(5

背叛残局 2024-07-22 23:30:03

我已经选择了像您上面描述的那样的心跳类型场景。 Ajax 轮询或 IFRAME。 当用户关闭浏览器并经过一定的超时(10 秒?)后,您可以将其注销。

另一种选择是让网站完全在 AJAX 上运行。 因此,用户只能访问一个“URL”,并且所有内容都是动态加载的。 当然,你会以这种方式破坏各种可用性的东西,但至少你实现了你的目标。

I have gone for a heartbeat type scenario like you describe above. Either Ajax Polling or an IFRAME. When the user closes the browser and a certain timeout elapses (10 seconds?), then you can log them out.

Another alternative would be to have the site run entirely on AJAX. Thus there is only one "URL" that a user can visit and all content is loaded dynamically. Of course you break all sorts of usability stuff this way, but at least you achieve your goal.

樱花落人离去 2024-07-22 23:30:03

如果用户关闭浏览器,或输入不同的 URL(包括选择最喜欢的 URL),则您无法检测到太多内容。

对于您网站上的链接,您可以创建通过您的网站转发的链接(即,而不是链接到 http://example.com /foo 您链接到 http://mysite.com /forwarder?dest=http://example.com/foo)。

请小心,仅转发到您想要转发的网站,否则您可能会因为“通用转发”被用于网络钓鱼等而引发安全问题。

If the user closes their browser, or types in a different URL (including selecting a favourite) there is not much for you to detect.

For links on your site, you could create links that forward via your site (i.e. rather than linking to http://example.com/foo you link to http://mysite.com/forwarder?dest=http://example.com/foo).

Just be careful to only forward to sites you intend to, otherwise you can open up security issues with "universal forwarding" being used for phishing etc..

沉溺在你眼里的海 2024-07-22 23:30:03

您绝对需要告诉客户这是不可能的。 他们对网络的运作方式存在着基本的误解。 显然,要外交一点……见鬼,这可能是别人的工作……但它需要完成。

您的建议或它们的组合可能会在简单的概念验证中发挥作用......但它们只会给您带来支持噩梦,并且不会足够一致地发挥作用。 更糟糕的是,毫无疑问,您还会造成用户由于安全黑客攻击而根本无法使用该应用程序的情况。

You absolutely, positively need to tell the client that this is not possible. They are having a basic misunderstanding of how the Web works. Be diplomatic, obviously... hell, it's probably someone else's job... but it needs to be done.

Your suggestions, or a combination of them, may work in a simple proof-of-concept... but they will bring you nothing but support nightmares and will not work consistently enough. Worse, you will undoubtably also create situations where users cannot use the application at all due to the security hacks misfiring on them.

何时共饮酒 2024-07-22 23:30:03

Javascript 有一个 onUnload 事件,当浏览器被告知离开页面时会触发该事件。 当您在编辑答案时尝试按后退按钮或单击链接时,您可以在 StackOverflow 上看到这一点。

您可以使用此事件触发站点的自动注销。

但是,我不确定这是否会处理故意关闭浏览器或浏览器进程从外部终止的情况(我猜第二种情况不会发生)。

Javascript has an onUnload event, which is triggered when the browser is told to leave the page. You can see this on StackOverflow when you try to press the back button or click a link while editing an answer.

You may use this event to trigger an auto-logoff for your site.

I am unsure, however, if this will handle cases wherein the browser is deliberately closed or the browser process externally terminated (I'm guessing it doesn't happen in the 2nd case).

哆兒滾 2024-07-22 23:30:03

如果您站点内的所有导航都是通过 .NET 回发(没有简单的 html 链接或 javascript 打开语句)完成的,则如果页面加载不是回发,您可以自动注销并重定向到登录页面。 这不会在退出时结束会话,但它看起来像这样,因为如果手动导航到您的 Web 应用程序,它会强制登录。 要为所有页面获取此功能,您可以使用在 Page_Load 中执行此操作的母版页。

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        System.Web.Security.FormsAuthentication.SignOut();
        System.Web.Security.FormsAuthentication.RedirectToLoginPage();
    }
}

If all navigation within your site is done through .NET postbacks (no simple html links or javascript open statements), you can do automatic logoff and redirect to the login page if the page load is not a postback. This does not end the session on exit, but it looks like it because it enforces a login if manually navigating to your web app. To get this functionality for all pages, you can use a Master page that does this in the Page_Load.

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        System.Web.Security.FormsAuthentication.SignOut();
        System.Web.Security.FormsAuthentication.RedirectToLoginPage();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文