簡體   English   中英

無論調用javascript函數如何,都會執行c#代碼

[英]c# code executed regardless of javascript function being called

我有一個問題,即使沒有執行功能注銷,用戶也始終注銷。 看來這是由於代碼在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> 

如Ryan所述,您的C#代碼將在頁面加載時運行。 我建議創建一個控制器方法Logout ,將代碼放在此處,然后在腳本中將logout()函數重定向到該方法。 代碼如下:
的HTML

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

Java腳本

    //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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM