处理 ASP.NET 中会话超时的最佳方法是什么
有多种方法可以处理会话超时,例如“元刷新”加载函数上的 javascript 等。
我想要一些简洁的东西,例如:超时前 5 分钟,警告用户...
我也在考虑保持会话打开只要浏览器已打开(尽管仍然需要弄清楚如何做到这一点......可能是一些具有刷新功能的 iframe)。
您如何处理会话超时,您认为我应该朝哪个方向发展?
There are various ways to handle session timeouts, like "meta refreshes" javascript on load functions etc.
I would like something neat like: 5 minutes before timeout, warn the user...
I am also contemplating keeping the session open for as long as the browser is open(still need to figure out how to do it though... probably some iframe with refreshing).
How do you handle session timeouts, and what direction do you think i should go in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理会话超时的最佳方法。
我说有两种基本情况。
一种情况是用户输入很少或没有数据,只是阅读报告,或者用鼠标进行一些小思考。在这种情况下,没有简单的方法来通知他会话即将过期。如果您要检查调用后面代码的会话的剩余时间,那么您会自动更新会话。然后,如果您有一个计时器来对会话进行倒计时,那么也许用户打开了您网站的新选项卡,并且会话即将过期,但您用 javascript 记下的时间不会过期,并且用户会收到错误的消息。
所以对我来说,当用户输入很少或没有数据时,就让会话过期,如果他失去了一次点击,稍后它会再次执行。
第二是当用户需要输入许多数据时,有时可能需要时间(例如很长的文本)来编写和修复它。在这种情况下,我使用以下技术,并且不会让会话结束。
如何保持会话与浏览器一样长时间打开。
这是一个非常好的和简单的技术,我使用一个图像,在会话超时之前使用 JavaScript 重新加载它。
在第三种情况中,您可以这样做。我们关心会话是否仅在回发时过期。当用户输入一些数据并在回发时,应用程序将他重定向到登录页面,并且帖子丢失。
在第三种情况下,您可以捕获帖子数据并保存它们,直到用户重新登录。您在
global.asax
上捕获发布数据,这是在重定向到登录页面之前调用的函数,您可以在其中查看是否有发布数据以及登录所需的使用情况,然后保存发布数据,以太到新的重定向页面,以太到服务器(可能在会话上,可能在临时数据库上)。
现在,当用户再次登录后,您将他再次重定向到包含保存的帖子数据的最后一页,并且用户将按原样继续。
这里唯一的技巧是创建一个中间页面,用最后发布的数据和自动重定向 JavaScript 调用来呈现表单。
The best approach to handle sessions timeouts.
I say that there is 2 basic cases.
One is when the users enter little or no data, and just read reports, or do small thinks with his mouse. In this case there is not easy way to inform him that the session is going to expire. If you going to check the time left for the session calling the code behind, then automatically you update the session. Then if you have a timer to count down the session, then maybe the user have open a new tab of your web and the session is going to expired but not the time you have note with javascript and the user receive wrong message.
So for me, when the user enter little or no data, just let the session expired, if he lose one click, it will do it again later.
Second is when the user need to enter many data, that some time can take time, a long text for example, to write it and fix it. In this case I use the below technique and I am not let the session go out.
How to keep the session open as long as the browser.
Here is a very nice and simple technique, I use an image that I make an reload of it before the session is timeout using JavaScript.
In a third case, you can do this. We care if the session is expired only on post back. When the user have enter some data and on the post back the application is redirect him on the login page and the post lost.
In this third case you can capture the post data and saved them until the user re-login. You capture the post data on
global.asax
on theThis is the function that called before the redirect to the login page, and there you see if you have post data and the use required to login, you save that post data, ether to a new redirect page, ether to the server (maybe on session, maybe on your temporary database).
Now after the user is login again, you redirect him again to the last page with the saved post data, and the user is continue as it is.
The only trick here is to make a middle page, that render the form with the last posted data and an automatically redirect javascript call.
我唯一能想到的是在创建客户端计时器的页面上生成一些脚本,以便在接收并呈现页面时,它可以在 X 分钟后(即过期前 5 分钟)显示警报。
如果您希望会话保持活动状态,则可以使用通过 AJAX 定期调用的通用处理程序 (ASHX) 来实现此目的。这将有助于刷新会话,并且只要 AJAX 调用继续,它就应该保持活动状态。
示例“keepalive.ASHX”:
这是页面上调用它的脚本(为简单起见,使用 jQuery):
The only thing I can think of is to generate some script on the page that creates a client timer, so that when the page is received and rendered, it can show an alert X-minutes later (that is 5mins before expire).
If you'd rather have the session just keep itself alive, you can do this with a generic handler (ASHX) that you periodically call via AJAX. This will help refresh the session and it should stay alive for as long as the AJAX calls continue.
Example "keepalive.ASHX":
And here's the script on the page to call it (with jQuery for simplicity):
使用一些 jquery 关闭 web.config 中的会话超时变量。您可以使用 这个 Jquery 延迟技巧,当特定时间发生时(页面加载后 x 分钟),它会弹出一个 div,指出 x 分钟后会话超时。漂亮、干净而且非常简单。
关于会话超时,Codesleuth 的 ajax 调用将是完美的。
Use some jquery that keys off of your session timeout variable in the web.config. You can use this Jquery delay trick that when a specific time occurs (x number of minutes after load of the page), it pops up a div stating session timeout in x minutes. Nice, clean and pretty simple.
Regarding session timeout, Codesleuth's ajax call would be perfect.