C# ASP.NET 自动注销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15991652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:41:28  来源:igfitidea点击:

ASP.NET Automatic logout

c#asp.netasp.net-mvc

提问by

I am doing a group project with 4 other people. We are designing a job kiosk in ASP.NET in MVC4 with embedded c#.

我正在和另外 4 个人一起做一个小组项目。我们正在 MVC4 中的 ASP.NET 中设计一个带有嵌入式 c# 的工作亭。

I am working on having the system log the user out if they are idle for 10 minutes. I need some help on how to start coding a way for the system to log the user out.

我正在努力让系统在用户闲置 10 分钟时将其注销。我需要一些关于如何开始为系统编写注销用户的方法的帮助。

采纳答案by Linus Caldwell

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.

由于大多数技术以某种方式依赖会话,因此这在大多数情况下都有效。

回答by Ryan Sessions

the answer you are looking for is the one AliK suggested. 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.

您正在寻找的答案是 AliK 建议的答案。您希望在 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. 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 分钟超时时间。

回答by Vinny Brown

If you need to have them automatically logged out, start with Linus Caldwell's suggestion of setting the web.config session timeout. 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. 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. The other is to add a refresh header to each page.

如果您需要让它们自动注销,请从 Linus Caldwell 的设置 web.config 会话超时的建议开始。他的示例显示 30 分钟,因此您只需将其更改为 10。但是,用户不会知道他们已注销,直到他们实际尝试请求某些服务器资源。要让这种情况自动发生,您可以采用多种方法。这两种方式都涉及在超时期限到期后自动刷新页面。一种方法是使用 javascript 计时器。另一种是为每个页面添加一个刷新标题。

<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));
}

回答by shakib

If by Idle you mean absence of mouse and keyboard events from user, and i understand your requirement correctly, you should check the jquery-idleTimeoutplugin.

如果空闲是指用户没有鼠标和键盘事件,并且我正确理解您的要求,您应该检查jquery-idleTimeout插件。

Another good one is jQuery idleTimer Plugin

另一个好的是jQuery idleTimer Plugin

hope this helps.

希望这可以帮助。

回答by Cyberdrew

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>