简体   繁体   English

ASP.NET自动注销

[英]ASP.NET Automatic logout

I am doing a group project with 4 other people. 我和另外4个人一起做团体项目。 We are designing a job kiosk in ASP.NET in MVC4 with embedded c#. 我们正在使用嵌入式c#在MVC4中设计ASP.NET中的作业信息亭。

I am working on having the system log the user out if they are idle for 10 minutes. 我正在努力让系统在闲置10分钟后将用户注销。 I need some help on how to start coding a way for the system to log the user out. 我需要一些帮助,如何开始编码系统记录用户的方式。

If you don't use "Windows Authentication", this at least depends on the session timeout you can control via web.config: 如果您不使用“Windows身份验证”,这至少取决于您可以通过web.config控制的会话超时:

<configuration>
    <system.web>
        <sessionState timeout="10" />
    </system.web>
</configuration>

As most techniques somehow rely on sessions, this will work in most scenarios. 由于大多数技术在某种程度上依赖于会话,因此这在大多数情况下都适用

the answer you are looking for is the one AliK suggested. 您正在寻找的答案是AliK建议的答案。 You want to set an automatic timeout in the web.config so that it will automatically logout the user and redirect them to the login page after a certain amount of idle time. 您希望在web.config中设置自动超时,以便它会在一定的空闲时间后自动注销用户并将其重定向到登录页面。

<authentication mode="Forms">
   <forms loginUrl="Login.aspx" protection="All" timeout="1" slidingExpiration="true">
   </forms>
</authentication>

If I remember right, the timeout value is in minutes, not seconds or milliseconds. 如果我没记错,超时值以分钟为单位,而不是秒或毫秒。 Also the sliding Expiration means that the timeout will reset each time you perform an action on the website. 滑动Expiration也意味着每次在网站上执行操作时都会重置超时。 So if you have a timeout of 5 minutes and you sit idle for 4 before clicking a button on the site then after the button click you get a new 5 minute timeout. 因此,如果您有5分钟的超时并且在点击网站上的按钮之前闲置4,那么在按钮单击后,您将获得新的5分钟超时。

Here is how I do it if you are using FormsAuthentication: 如果您使用FormsAuthentication,我就是这样做的:

Controller Action: 控制器动作:

public ActionResult CheckLogin()
{
    if (Request.Cookies["CookieName"] == null) return Json(0, JsonRequestBehavior.AllowGet);
    var cookie = Request.Cookies["CookieName"].Value;
    var ticket = FormsAuthentication.Decrypt(cookie);
    var secondsRemaining = Math.Round((ticket.Expiration - DateTime.Now).TotalSeconds, 0);
    return Json(secondsRemaining, JsonRequestBehavior.AllowGet);
}

Jquery on each page or on layout page: 每页或布局页面上的Jquery:

<script>
    $(function () {
        setTimeout(doStuff, 1000);
    });
    function doStuff() {
        $.ajax("/CheckLogin").done(function (data) {
            if (data <= 60) {
                startLogout(data);
            } else {
                setTimeout(doStuff, 1000);
            }
        });
    }

    function startLogout(seconds) {
        var countdown = setInterval(function () {
            //Show something here
            if (count == 0) {
                clearInterval(countdown);
                //Do something here
            }
            seconds--;
        }, 1000);
    }
</script>

If you need to have them automatically logged out, start with Linus Caldwell's suggestion of setting the web.config session timeout. 如果您需要让它们自动注销,请从Linus Caldwell建议设置web.config会话超时开始。 His example shows 30 minutes, so you would just change it to 10. The user won't know that they're logged out, though, until they actually try to request some server resource. 他的示例显示了30分钟,因此您只需将其更改为10.用户不会知道他们已经注销,直到他们实际尝试请求某些服务器资源。 To have that happen automatically, you can go a couple of ways. 为了让这种情况自动发生,你可以采取几种方式。 Both of these ways involve automatically refreshing the page after the timeout period has expired. 这两种方法都涉及在超时期限到期后自动刷新页面。 One way is to use a javascript timer. 一种方法是使用javascript计时器。 The other is to add a refresh header to each page. 另一种是为每个页面添加刷新标头。

<script type="text/javascript">
var seconds = 60 * 11;// set timer for 11 minutes (1 minutes after session expires)
countdown();
function countdown(){
   seconds--;
   if (seconds <= 0){
          window.location.reload(); // force a refresh.
   }else{
          setTimeout('countdown()', 1000);
   }
}
</script>

The other way would be in your global.asax: 另一种方式是在你的global.asax中:

protected void Application_BeginRequest()
{
    Response.Headers.Add("Refresh", Convert.ToString(Session.Timeout * 11));
}

If by Idle you mean absence of mouse and keyboard events from user, and i understand your requirement correctly, you should check the jquery-idleTimeout plugin. 如果通过Idle你意味着没有来自用户的鼠标和键盘事件,并且我正确理解你的要求,你应该检查jquery-idleTimeout插件。

Another good one is jQuery idleTimer Plugin 另一个好的是jQuery idleTimer插件

hope this helps. 希望这可以帮助。

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

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