简体   繁体   English

闲置后重定向用户,但保持会话活动

[英]redirect user after inactivity, but keep session alive

I try to redirect my page for a logged in user with a certain amount of inactivity to a different page without killing the session. 我尝试将登录页面的用户以一定程度的不活动状态重定向到其他页面,而不会终止会话。

I would like to substract the time I spent on the first page from the session, put the user on the new page, and then log the user out after his session (rest of the session time) times out and redirect the user to the login page. 我想减去我在会话第一页上花费的时间,将用户放在新页面上,然后在会话超时(会话剩余时间)后将用户注销,然后将用户重定向到登录名页。

I found this: 我找到了这个:

HttpContext.Current.Response.AppendHeader("Refresh", Convert.ToString(((HttpContext.Current.Session.Timeout * 2) - 5)) + "; Url=Dashboard.aspx");

but this interferes with my master page: 但这会干扰我的母版页:

Context.Response.AppendHeader("Refresh",Convert.ToString((Session.Timeout * 60)) + "; URL=" + ResolveUrl("~/Logout.aspx")); 

If it is easier, the user session does not need to be subtracted by the time the user spent on the first page. 如果更简单,则无需将用户会话数减去用户在第一页上花费的时间。

Is there maybe an easy javascript out there that I missed on google? 也许我在Google上错过了一个简单的JavaScript吗?

Thanks, Patrick 谢谢,帕特里克

Ok, I did the following to solve this issue: Everytime a user hits a button, i call detime(), and also on the page_load I call detime(). 好的,我执行以下操作来解决此问题:每当用户按下按钮时,我都会调用detime(),并且在page_load上也会调用detime()。 Maybe not the best solution, but at least I got what I want ;) 也许不是最好的解决方案,但至少我得到了我想要的;)

function timer() 
{
    time1 = window.setTimeout('redirect();', 300000);
}

function redirect() 
{
   window.location = "XXX.aspx";
}

function detime() 
{
    if (time1 !=null) {
        window.clearTimeout(time1);
        time1 = null;
    }
timer()
}

Well, I suppose it depends on your server platform, but in my experience it's the server that determines session timeout. 好吧,我想这取决于您的服务器平台,但是根据我的经验,是由服务器决定会话超时的。 The reload that your client-side code triggers will, unless you somehow override the normal behavior of the server, refresh the session and start the timer over again. 除非您以某种方式覆盖了服务器的正常行为,否则客户端代码触发的重载将刷新会话并重新启动计时器。

Now, what you could do is update the page client-side and not talk to the server at all. 现在,您可以做的是在客户端更新页面,而根本不与服务器对话。 To do that, you'd effectively load the code to the "almost timed out" page at the same time you load the original page. 为此,您可以在加载原始页面的同时有效地将代码加载到“几乎超时”页面。 Then, when your timeout fires, you just show the desired page and hide whatever's there. 然后,当超时触发时,您只显示所需的页面并隐藏其中的任何内容。

I would encourage you to consider the usability issues with this overall scheme. 我鼓励您考虑整个方案的可用性问题。

It seems like what you want to do is pass a timestamp along with the redirect and then capture the timestamp and either use that or, if its undefined, the current time as the time the user started. 似乎您想要做的是传递一个时间戳和重定向,然后捕获该时间戳,然后使用它,或者,如果未定义,则使用当前时间作为用户开始的时间。 Then once that time has run out send the user to an explicit log out that kills their session. 然后,一旦该时间用完,请将用户发送到显式注销,这将终止其会话。

As long as you redirect to a page that explicitly kills the session, I don't think you'll have a problem with expiration. 只要您重定向到显式终止该会话的页面,我认为您不会遇到过期问题。 Accept in the case where the user gets redirected to the new page, and then closes the browser, which maybe you could use an onunload script to kill the session explicitly again. 如果用户被重定向到新页面,然后关闭浏览器,请接受,也许您可​​以使用onunload脚本再次明确终止会话。

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

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