首页 > 代码库 > asp.net实现自动logout

asp.net实现自动logout

  用户登录系统后,如果没有进行任何操作,规定时间后,系统会自动弹出对话框,提示用户重新登录。

代码如下:

<script type="text/javascript">
        var timer;
        var wait = 20; // waiting time in minutes
        document.onmousemove = resetTimer;
        function resetTimer() {
            var timeoutPopup_display = document.getElementById(‘div_timeoutPopup‘).style.display;
            if (timeoutPopup_display != "block") {
                clearTimeout(timer);
            }

            timer = setTimeout("logout()", 60000 * wait); //60000表示60秒
        }
        function logout() {
            document.getElementById(‘div_background‘).style.display = ‘block‘;
            document.getElementById(‘div_timeoutPopup‘).style.display = ‘block‘;
            document.getElementById(‘expired_text‘).style.display = ‘block‘;
            document.getElementById(‘btn_logout‘).style.display = ‘inline-block‘;           
        }  
    </script>

在前台界面上增加两个div

<div id="div_background" style="display:none; width:100%;height:100%; position:fixed;top:0px;left:0px;
      background-color:#030303; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=80);filter:alpha(opacity=80);opacity:0.8;">   
    </div>
    <div id="div_timeoutPopup" align="center" class="timeoutPopup">       
        <span id="expired_text" style="font-size:14px;display:none"><br /><br /><br />Your session has expired!<br /><br /><br /></span>       
        <input type="button" id="btn_logout" value=http://www.mamicode.com/"Log-in Again" style="height:30px;width:100px;display:none;" onclick="location.href=‘AdminLogin.aspx‘" />
    </div>

css代码如下:

.timeoutPopup
{
    display:none;
    position:fixed;
    top:0; right:0; bottom:0; left:0;
    width:30%;
    height:25%;
    margin:auto;
    padding:4px;
    border:solid 2px #999999;
    background-color:#EAEAEA;
    text-align:center;
}

效果如图

技术分享



asp.net实现自动logout