简体   繁体   English

在asp.net + jquery中实现自动注销+警告?

[英]Implement auto log-out + warning in asp.net + jquery?

Many sites ( Bank webSite for example) - implement log-out + 1 minute warning before session is about to expire.( 20 minutes) 许多站点(例如Bank webSite) - 在会话即将到期之前实施注销+ 1分钟警告。(20分钟)

( this topic is not discussed much - the only question ive seen is with using asp.net membership - which I don't use ) 这个主题没有多少讨论 - 我见过的唯一问题是使用asp.net会员资格 - 我不使用

each user will have a session["lastActionTime"] 每个用户都有一个session["lastActionTime"]

this session will be update to current Time when : 此会话将更新到当前时间

  • Page is loaded 页面已加载
  • Ajax request has executed ( due to user action) Ajax请求已执行(由于用户操作)

Now - when a page loads , I set the session value. 现在 - 当页面加载时,我设置会话值。 (lets say 19:00) (比如19:00)

Also , for every ajax request (my site doesnt create postbacks - only ajax jquery) - I use an ASHX handler with IRequiresSessionState which updates the session to current Time. 此外,对于每个ajax请求(我的网站不创建回发 - 只有ajax jquery) - 我使用带有IRequiresSessionState的ASHX处理程序, IRequiresSessionState会话更新为当前时间。

I use something like this : 我使用这样的东西:

jQuery(document).ajaxStart(function(){
    gotoHandlerAndUpdateSessionTime();
})

Now -the part for 1 minute before warning message ( " your session is about to expire ") : 现在 - 警告信息前1分钟的部分(“ 你的会话即将到期 ”):

Every ajax return event or page load event - I activate in javascript : setInterval with [sessionTime-1] minutes ( 20-1=19). 每个ajax返回事件或page load事件 - 我在javascriptsetInterval激活[sessionTime-1]分钟(20-1 = 19)。 ( and of course - cancelling all prev setIntervals... ) (当然 - 取消所有上一个setIntervals ...)

now when the event (setInterval) occurs - it is 1 minute before expiration time : (19 min) 现在当事件(setInterval)发生时 - 它在到期时间前1分钟:( 19分钟)

I display a warning div , and the user can choose exit or stay . 我显示一个警告div,用户可以选择退出停留

question : 题 :

1) what if the user didnt press nothing on the warning div , How ( after 1 minute from displaying the div ) will I log him out ? 1)如果用户没有在警告div上按任何内容,如何( 显示div后1分钟 )我会将他记录下来怎么办? Should I open a setTimeout of 1 minute when displaying the div and then (if nothing pressed) to log him out ? 我应该在显示div时打开1分钟的setTimeout然后(如果没有按下)将其记录下来?

2) is it the right way of doing it ? 2)这是正确的做法吗?

3) Shouldn't there be cookies in this whole weird story ? 3)这整个奇怪的故事不应该有饼干吗? :-) :-)

(please - no membership - or Forms authentication). (请 - 没有会员资格 - 或表格认证)。 I'm tagging this question also as PHP since I know it is relevant to php programmers as well and I would like to hear from their knowledge. 我也将这个问题标记为PHP,因为我知道它也与php程序员有关,我想听听他们的知识。

Royi, to answer both of your questions, I would say YES. 罗伊,回答你的两个问题,我会说是的。 I've built these several times (usually with Forms Auth), but basically you have a timer that counts down to show the first warning, and then another timer that counts down and gives the user X seconds to answer. 我已经多次构建这些(通常使用Forms Auth),但基本上你有一个计时器倒计时显示第一个警告,然后另一个计时器倒计时并给用户X秒回答。 I usually put the X second count down on the warning message so they can see how much time they have left. 我通常会在警告信息上放下第二次计数,这样他们就可以看到他们还剩下多少时间。 If they don't answer in the allotted time, a call gets made to Logout.ashx (or whatever) that destroys the session and then the javascript can redirect them back to the login page. 如果他们没有在规定的时间内回答,则会调用Logout.ashx(或其他)来销毁会话,然后javascript可以将它们重定向回登录页面。 I hope that helps. 我希望有所帮助。

Regarding your third question, as long as you're tracking the session you shouldn't really need cookies. 关于你的第三个问题,只要你跟踪会话,你就不应该真正需要cookie。 Just do a session_destroy() in PHP or Session.Abandon() in C# when the javascript timer counts down. 当javascript计时器倒计时时,只需在PHP中执行session_destroy()或在C#中执行Session.Abandon()。

Here's some code I'm using on one of my sites (might not be the cleanest, but you get the idea): 这是我在我的一个网站上使用的一些代码(可能不是最干净的,但你明白了):

var timeoutPolled = 0;
var timeoutSeconds = 10;
var countDownCounter = 61;
var timeoutBetweenPolls = 5000;
var stopCountDown = false;

function InitializePollTimer(timeoutMinutes) {
    timeoutSeconds = timeoutMinutes * 60;

    StartPollTimer();
}

function StartPollTimer() {
    setTimeout(PollForTimeout, timeoutBetweenPolls);
}

function PollForTimeout() {
    timeoutPolled++;

    if ((timeoutPolled * timeoutBetweenPolls) > 1 * (timeoutSeconds * 1000)) {
        $("#timeoutDialog").dialog({
            autoOpen: false,
            bgiframe: true,
            resizable: false,
            height: 250,
            draggable: false,
            modal: true,
            zindex: 99999999,
            position: 'top',   
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },       
            buttons: {
                "Continue using Website?": function() {
                    StopCountDown();

                    $.ajax({
                        type: "GET",
                        url: "RefreshSession.aspx",
                        cache: false
                    });

                    $(this).dialog("close");

                    timeoutPolled = 0;
                    StartPollTimer();
                },
                "Logout": function() {
                    Logout();
                }
            }
        });

        $("#timeoutDialog").dialog("open");

        countDownCounter = 61;

        CountDown();
    }
    else {
        StartPollTimer();
    }
}

function CountDown() {
    if (stopCountDown) {
        stopCountDown = false;
    }
    else {
        countDownCounter--;
        $("#countdownTimer").html(countDownCounter);

        if (countDownCounter > 0) {
            setTimeout(CountDown, 950);
        }
        else {
            Logout();
        }
    }
}

function StopCountDown() {
    stopCountDown = true;
}

function Logout() {
    window.location.href = 'Logout.aspx';
}

It's possible I'm not going to tell you anything you don't already know but here's my two cents anyway - 我可能不会告诉你任何你不知道的东西,但这里是我的两分钱 -

On the user not pressing the warning button there are two areas: (1) the information being currently displayed on the screen (2) additional requests for more informaion being blocked - for the first, a javascript timer should forward to a LoggedOut page if not canceled by the posative result of clicking on the warning dialog and for the second, definately server side logic should be checking the current request for being in the context of a logged in user. 在没有按下警告按钮的用户上有两个区域:(1)当前显示在屏幕上的信息(2)阻止更多信息的附加请求 - 对于第一个,javascript计时器应转发到LoggedOut页面,如果不是通过单击警告对话框的结果取消,对于第二个,肯定的服务器端逻辑应该检查当前请求是否在登录用户的上下文中。

As for cookies, it sounds like you're using on already to if you haven't altered the default settings for session state - i think it's called .ASPNET but I could be wrong you can check with a proxy tool. 对于cookie,如果你没有改变会话状态的默认设置,听起来你已经在使用了 - 我认为它被称为.ASPNET但我可能错了你可以用代理工具检查。

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

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