0

我有一个问题,即使功能注销尚未执行,用户也一直注销。这似乎是由于代码在 c# 中(这是在剃刀布局页面 mvc4 中)

<script type="text/javascript">
    var idleTimer = 0;
    function notIdle() 
    {
        clearTimeout(idleTimer);     
        idleTimer = setTimeout(function () { logout() }, 5000);
    }

    function logout()           
    {
        @if (Request.IsAuthenticated )
        {
            WebSecurity.Logout();                  
        }
        window.location.reload();
        clearTimeout(idleTimer);

    }         
</script> 
4

1 回答 1

0

正如 Ryan 所说,您的 C# 代码将在页面加载时运行。我可以建议制作一个控制器方法Logout,将您的代码放在那里,然后在您的脚本logout()函数中重定向到该方法。代码将是这样的:
HTML

<button onclick="logout()">Logout</button>

Javascript

    //I don't really know your logic behind those timeouts, so I leave them as is
    var idleTimer = 0;
    function notIdle() 
    {
        clearTimeout(idleTimer);     
        idleTimer = setTimeout(function () { logout() }, 5000);
    }

    function logout()           
    {
        location.href = "Home/Logout"; 
        //"Home" is the name of your controller, where you will put the Logout method.
    } 

C#

public ActionResult Logout()
{
    if (Request.IsAuthenticated)
    {
        WebSecurity.Logout();
    }
    return RedirectToAction("Index", "Home"); //redirect to your previous page
}
于 2013-04-16T20:01:16.613 回答