用户在asp.net中空闲时的会话超时警告 [英] Session timeout warning when the user is idle in asp.net

查看:65
本文介绍了用户在asp.net中空闲时的会话超时警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net开发一个Web应用程序,当用户在整个项目的页面上处于非活动状态时。我应该在弹出窗口中收到一条警告消息,就像你的会话即将在几秒钟内过期一样。在该消息框中,我应该有两个名为保持登录和注销的按钮。当我点击保持登录按钮时,页面应该变为活动状态,弹出窗口应该关闭。当我点击注销时它应该重定向到登录页面。即使在时间计数结束之前没有点击任何按钮,我也应该被重定向到登录页面。



我尝试了什么:



I am developing a web application using an asp.net, When the user is inactive on the page through out the project. i should get a warning message in a popup window like your session is about to expire in some seconds. in that message box i should have two buttons named ''stay log in" and "log out". when i click on "stay log in button" the page should become active and popup should close. when i click on "log out" it should redirect to login page. Even not clicked any button before time count ends i should be redirected to login page.

What I have tried:

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/themes/ui-lightness/jquery-ui.css" type="text/css" />
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .modalBackground
        {
            background-color: Black;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
        .modalPopup
        {
            background-color: #FFFFFF;
            width: 300px;
            border: 3px solid #0DA9D0;
            border-radius: 12px;
            padding: 0;
        }
        .modalPopup .header
        {
            background-color: #2FBDF1;
            height: 30px;
            color: White;
            line-height: 30px;
            text-align: center;
            font-weight: bold;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
        }
        .modalPopup .body
        {
            padding: 10px;
            min-height: 50px;
            text-align: center;
            font-weight: bold;
        }
        .modalPopup .footer
        {
            padding: 6px;
        }
        .modalPopup .yes, .modalPopup .no
        {
            height: 23px;
            color: White;
            line-height: 23px;
            text-align: center;
            font-weight: bold;
            cursor: pointer;
            border-radius: 4px;
        }
        .modalPopup .yes
        {
            background-color: #2FBDF1;
            border: 1px solid #0DA9D0;
        }
        .modalPopup .no
        {
            background-color: #9F9F9F;
            border: 1px solid #5C5C5C;
        }
    </style>
    <script type="text/javascript">
        var timoutWarning = 40000;
       
        var warningTimer;
        
        function StartTimers() {
            warningTimer = setTimeout("SessionExpireAlert()", timoutWarning);
            
        }
        
        function SessionExpireAlert() {

            var timeout = 20000;
            
            var seconds = timer / 1000;
           
            
            document.getElementsByName("seconds").innerHTML = seconds;
            setInterval(function () {
                seconds--;
                document.getElementById("seconds").innerHTML = seconds;

            },1000);
            setTimeout(function () {
                //Show Popup before 20 seconds of timeout.
                $find("mpeTimeout").show();
            }, timeout - 19 * 1000);
            setTimeout(function () {
                
                window.location = "Default.aspx";
            }, timeout);
        };
        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        }
        function ResetTimers() {
            clearTimeout(seconds);
            SessionExpireAlert
                       }
        
    </script>
</head>
<body >
    <form id="form1"  runat="server" >
        <asp:ScriptManager ID="ScriptManager1" runat="server">
    <div>
        <asp:LinkButton ID="lnkFake" runat="server" />
        <ajaxToolkit:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout"  runat="server" PopupControlID="pnlPopup"

        TargetControlID="lnkFake" OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground"

        >
    <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
        <div[>
            Session Expiring!
        
        <div class="body">
            Your Session will expire in <span id="seconds"></span> seconds.<br />
            Do you want to reset?
        </div>
        <div class="footer" align="right">
            <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" />
            <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" />
        </div>
    
    
    </div>
    </form>
</body>
</html>

推荐答案

find(< /跨度> mpeTimeout)。show();
},超时 - 19 * 1000 );
setTimeout( function (){

window location = Default.aspx;
},超时);
};
function ResetSession(){
// 重定向以刷新会话。
window location = window location .href;
}
function ResetTimers(){
clearTimeout(seconds);
SessionExpireAlert
}

< / < span class =code-leadattribute> script >
< / head >
< 正文 >
< 表格 id = form1 runat = server >
< asp:ScriptManager ID = ScriptManager1 runat = 服务器 >
< span class =code-keyword>< div >
< asp:LinkBut​​ton ID = lnkFake runat = server / >
< ajaxToolkit: ModalPopupExtender ID = mpeTimeout BehaviorID = mpeTimeout runat = server PopupControlID = pnlPopup

TargetControlID = lnkFake OkControlID = btnYes CancelControlID = btnNo BackgroundCssClass = modalBackground

> ;
< asp:Panel ID = pnlPopup runat = server CssClass = modalPopup 样式 = display:none >
< div [ < span class =code-keyword>>

会话到期!

< div class = body >
您的会话将在< span id=\"seconds\"><< span class=\"code-leadattribute\">/span> seconds.<< span class=\"code-leadattribute\">br />
Do you want to reset?
</div>
<div class=\"footer\" align=\"right\">
<asp:Button ID=\"btnYes\" runat=\"server\" Text=\"Yes\" CssClass=\"yes\" />
<asp:Button ID=\"btnNo\" runat=\"server\" Text=\"No\" CssClass=\"no\" />
</div>


</div>
</form>
</body>
</html>
find("mpeTimeout").show(); }, timeout - 19 * 1000); setTimeout(function () { window.location = "Default.aspx"; }, timeout); }; function ResetSession() { //Redirect to refresh Session. window.location = window.location.href; } function ResetTimers() { clearTimeout(seconds); SessionExpireAlert } </script> </head> <body > <form id="form1" runat="server" > <asp:ScriptManager ID="ScriptManager1" runat="server"> <div> <asp:LinkButton ID="lnkFake" runat="server" /> <ajaxToolkit:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout" runat="server" PopupControlID="pnlPopup" TargetControlID="lnkFake" OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground" > <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none"> <div[> Session Expiring! <div class="body"> Your Session will expire in <span id="seconds"></span> seconds.<br /> Do you want to reset? </div> <div class="footer" align="right"> <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" /> <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" /> </div> </div> </form> </body> </html>


check below link, hope it will help you.



Automatically redirect User after Session Timeout in ASP.Net
check below link, hope it will help you.

Automatically redirect User after Session Timeout in ASP.Net


c# - Cookie Confusion with FormsAuthentication.SetAuthCookie() Method - Stack Overflow[^]



The parameter timeout you’ve found in /system.web/authentication/forms is the timeout (in minutes) of the duration of authentication ticket.



This means that after a certain amount of time of inactivity, a user is prompted to login again. If you try to check this My.Profile.Current.IsAuthenticated it will be false.



You can choose not to persist the cookie. In this situation if your ticket expires, your cookie expires too. The cookie (in case is persisted) has a purpose to remember the user if he/she comes back to your site.



You might want to persist your cookie for 10 years so the user will never have to insert username and password again, unless they’ve chosen to delete the cookie. The cookie is valid even if the browser is closed (when it is persisted).



Another important thing to remember is the parameter slidingExpiration:

if it’s true your authentication ticket will be renewed every time there’s activity on your site: refresh of the page etc.



What you can do - and what I’ve done - is to write your own cookie like this:



c# - Cookie Confusion with FormsAuthentication.SetAuthCookie() Method - Stack Overflow[^]

The parameter timeout you've found in /system.web/authentication/forms is the timeout (in minutes) of the duration of authentication ticket.

This means that after a certain amount of time of inactivity, a user is prompted to login again. If you try to check this My.Profile.Current.IsAuthenticated it will be false.

You can choose not to persist the cookie. In this situation if your ticket expires, your cookie expires too. The cookie (in case is persisted) has a purpose to remember the user if he/she comes back to your site.

You might want to persist your cookie for 10 years so the user will never have to insert username and password again, unless they've chosen to delete the cookie. The cookie is valid even if the browser is closed (when it is persisted).

Another important thing to remember is the parameter slidingExpiration:
if it's true your authentication ticket will be renewed every time there's activity on your site: refresh of the page etc.

What you can do - and what I've done - is to write your own cookie like this:

FormsAuthenticationTicket authTicket = new
     FormsAuthenticationTicket(1, //version
     userName, // user name
     DateTime.Now,             //creation
     DateTime.Now.AddMinutes(30), //Expiration (you can set it to 1 month
     true,  //Persistent
     userData); // additional informations







Differences: slidingExpiration vs time Expiration vs Persistent cookie ?



If IIS App Pool recyles? Shutdown worker process if idle and recycle worker process Authentication cookie not expires, Session expires.



If modify Global.asax or Web.config, or Bin folder ?




Differences: slidingExpiration vs time Expiration vs Persistent cookie ?

If IIS App Pool recyles? Shutdown worker process if idle and recycle worker process Authentication cookie not expires, Session expires.

If modify Global.asax or Web.config, or Bin folder ?


这篇关于用户在asp.net中空闲时的会话超时警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆