如何只为偶像用户做会话超时?

Posted

技术标签:

【中文标题】如何只为偶像用户做会话超时?【英文标题】:How to do session timeout only for idol users? 【发布时间】:2021-03-21 17:48:08 【问题描述】:

即使用户处于活动状态并执行操作(即点击屏幕),我的代码也会每 2 分钟显示一次超时计数器。我只想为偶像用户执行超时。我怎样才能在我的代码中实现它。 我创建了 2 个网络表单 default.aspxsessionTime.aspx 和 1 个包含 jQuery 函数 .$timeoutDialog 函数的 JS 文件。

web.config 包含:

<sessionState mode="InProc" timeout="2"/>

timeout-dialog.js:

String.prototype.format = function() 
    var s = this,
      i = arguments.length;

    while (i--) 
        s = s.replace(new RegExp('\\' + i + '\\', 'gm'), arguments[i]);
    
    return s;
;

!function($) 
    $.timeoutDialog = function(options) 

        var settings = 
            timeout: 1200,
            countdown: 60,
            title: 'Your session is about to expire!',
            message: 'You will be logged out in 0 seconds.',
            question: 'Do you want to stay signed in?',
            keep_alive_button_text: 'Yes, Keep me signed in',
            sign_out_button_text: 'No, Sign me out',
            keep_alive_url: '/keep-alive',
            logout_url: null,
            logout_redirect_url: '/',
            restart_on_yes: true,
            dialog_width: 350
        

        $.extend(settings, options);

        var TimeoutDialog = 
            init: function() 
                this.setupDialogTimer();
            ,

            setupDialogTimer: function() 
                var self = this;
                window.setTimeout(function() 
                    self.setupDialog();
                , (settings.timeout - settings.countdown) * 1000);
            ,

            setupDialog: function() 
                var self = this;
                self.destroyDialog();

                $('<div id="timeout-dialog">' +
            '<p id="timeout-message">' + settings.message.format('<span id="timeout-countdown">' + settings.countdown + '</span>') + '</p>' +
            '<p id="timeout-question">' + settings.question + '</p>' +
          '</div>')
        .appendTo('body')
        .dialog(
            modal: true,
            width: settings.dialog_width,
            minHeight: 'auto',
            zIndex: 10000,
            closeOnEscape: false,
            draggable: false,
            resizable: false,
            dialogClass: 'timeout-dialog',
            title: settings.title,
            buttons: 
                'keep-alive-button': 
                    text: settings.keep_alive_button_text,
                    id: "timeout-keep-signin-btn",
                    click: function() 
                        self.keepAlive();
                    
                ,
                'sign-out-button': 
                    text: settings.sign_out_button_text,
                    id: "timeout-sign-out-button",
                    click: function() 
                        self.signOut(true);
                    
                
            
        );

                self.startCountdown();
            ,

            destroyDialog: function() 
                if ($("#timeout-dialog").length) 
                    $(this).dialog("close");
                    $('#timeout-dialog').remove();
                
            ,

            startCountdown: function() 
                var self = this,
            counter = settings.countdown;

                this.countdown = window.setInterval(function() 
                    counter -= 1;
                    $("#timeout-countdown").html(counter);

                    if (counter <= 0) 
                        window.clearInterval(self.countdown);
                        self.signOut(false);
                    

                , 1000);
            ,

            keepAlive: function() 
                var self = this;
                this.destroyDialog();
                window.clearInterval(this.countdown);

                $.get(settings.keep_alive_url, function(data) 
                    if (settings.keep_alive_url != null) 
                        self.redirectLogin();
                        //                        $.post(settings.keep_alive_url, function(data) 
                        //                            
                        //                        );
                    
                    else 
                        if (data == "OK") 
                            if (settings.restart_on_yes) 
                                self.setupDialogTimer();
                            
                        
                        else 
                            self.signOut(false);
                        
                    
                );
            ,

            signOut: function(is_forced) 
                var self = this;
                this.destroyDialog();

                if (settings.logout_url != null) 
                    $.post(settings.logout_url, function(data) 
                        self.redirectLogout(is_forced);
                    );
                
                else 
                    self.redirectLogout(is_forced);
                
            ,

            redirectLogout: function(is_forced) 
                //var target = settings.logout_redirect_url + '?next=' + encodeURIComponent(window.location.pathname + window.location.search);
                var target = settings.logout_redirect_url;
                //                if (!is_forced)
                //                    target += '&timeout=t';
                window.location = target;
            ,

            redirectLogin: function() 
                //var target = settings.logout_redirect_url + '?next=' + encodeURIComponent(window.location.pathname + window.location.search);
                var target = settings.keep_alive_url;
                //                if (!is_forced)
                //                    target += '&timeout=t';
                window.location = target;
            
        ;

        TimeoutDialog.init();
    ;
 (window.jQuery);
timeout-dialog.css
    /* Some default button styles */
    button font-size: 100%; margin: 0; vertical-align: baseline; *vertical-align: middle;
    button line-height: normal; *overflow: visible;
    button cursor: pointer; -webkit-appearance: button;

    button 
      -webkit-border-radius: 4px;
      -moz-border-radius: 4px;
      border-radius: 4px;
      border: 1px solid #cccccc;
      border-width: 1px;
      border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
      -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5), inset 0 1px 1px rgba(0, 0, 0, 0.1);
      -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5), inset 0 1px 1px rgba(0, 0, 0, 0.1);
      box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5), inset 0 1px 1px rgba(0, 0, 0, 0.1);
      cursor: pointer;
      color: #333333;
      display: inline-block;
      font-size: 14px;
      line-height: normal;
      padding: 5px 10px;
      text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    

    .button:hover, button:hover 
      text-decoration: none;
      background-position: 0 -15px;
    

    .button:focus, button:focus 
      -webkit-box-shadow: 0 0px 2px rgba(0, 0, 0, 0.4);
      -moz-box-shadow: 0 0px 2px rgba(0, 0, 0, 0.4);
      box-shadow: 0 0px 2px rgba(0, 0, 0, 0.4);
      outline: none;
    

    /* Timeout Dialog Styles */
    .timeout-dialog 
      padding: 15px;
      position: absolute;
      background: #eeeeee url("../imgs/timeout-icon.png") no-repeat 15px 25px;
      border: 1px solid #ffffff;
      -webkit-box-shadow: 0 0px 5px rgba(0, 0, 0, 0.5);
      -moz-box-shadow: 0 0px 5px rgba(0, 0, 0, 0.5);
      box-shadow: 0 0px 5px rgba(0, 0, 0, 0.5);
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border-radius: 5px;
    
    .timeout-dialog .ui-dialog-title 
      font-size: 16px;
      font-weight: bold;
      display: block;
      padding: 0 0 15px 0;
      margin-left: 80px;
    
    .timeout-dialog .ui-dialog-titlebar-close 
      display: none;
    
    .timeout-dialog .ui-dialog-buttonpane 
      margin-top: 15px;
    
    .timeout-dialog  ~ .ui-widget-overlay 
      position: absolute;
      top: 0;
      left: 0;
      background-color: #000;
      filter: alpha(opacity=40);
      opacity: 0.4;
    
    .timeout-dialog p 
      margin: 0 0 5px 80px;
    
    #timeout-keep-signin-btn 
      color: #FFF;
      background-color: #0f5895;
      background-repeat: repeat-x;
        color: #ffffff;
        text-shadow: none;
        margin: 5px 10px 5px 0;
        background-image: linear-gradient(top, #377bb2, #0f5895);
    
    #timeout-sign-out-button 
      color: #FFF;
      background-color: #e6e6e6;
      background-repeat: repeat-x;
        color: #000000;
        text-shadow: none;
        margin: 5px 0;
        background-image: linear-gradient(top, #ffffff, #e6e6e6);
    
    #timeout-countdown 
      font-weight: bold;
    
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="popup.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Session Time Out Warning Message</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>

    <script src="Script/timeout-dialog.js" type="text/javascript"></script>

    <link href="css/timeout-dialog.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">

        function Timer(time) 
            setTimeout(TimeOut, time);
        

        function TimeOut() 
            $.timeoutDialog(
                timeout: 1,
                countdown: 60,
                keep_alive_url: window.location.pathname,
                logout_redirect_url: 'SessionTime.aspx',
                restart_on_yes: true
            );
        

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

默认.aspx.cs

namespace popup

    public partial class Default : System.Web.UI.Page
    
        protected void Page_Load(object sender, EventArgs e)
        
            if (!IsPostBack)
            
                int _displayTimeInMiliSec = (Session.Timeout - 1) * 60000;

                if (Session["ID"] == null)
                
                    Session["ID"] = "New Session";
                    lblMsg.Text = Convert.ToString(Session["ID"]);
                
                else
                    lblMsg.Text = "Old Session";

                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(),
                    "message",
                    "<script type=\"text/javascript\" language=\"javascript\">Timer('" + _displayTimeInMiliSec + "');</script>",
                    false);
            
        
    

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionTime.aspx.cs" Inherits="popup.SessionTime" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Session Time Out
    </div>
    </form>
</body>
</html>

SessionTime.aspx.cs

namespace popup

    public partial class SessionTime : System.Web.UI.Page
    
        protected void Page_Load(object sender, EventArgs e)
        
            Session["ID"] = null;
            Session.Abandon();
        
    

【问题讨论】:

有人起来了吗? 【参考方案1】:

会话超时已经是 IIS 和 WebForms 的一项功能。 有几种配置方式,这里是一种方式

https://docs.microsoft.com/en-us/dotnet/api/system.web.sessionstate.httpsessionstate.timeout?view=netframework-4.8

【讨论】:

以上是关于如何只为偶像用户做会话超时?的主要内容,如果未能解决你的问题,请参考以下文章

如何在会话超时或结束时注销用户

如何在会话超时时关闭所有活动的引导模式?

会话超时后如何将用户重定向到自定义网页?

春季/安全中的自定义会话超时

处理网络应用程序中会话超时的最佳方法?

如何解决会话超时问题?