c#不管调用javascript函数都会执行代码
我有一个问题,用户总是注销,即使功能注销尚未执行。这似乎是由于代码在c#中(这是在razor布局页面mvc4中)c#不管调用javascript函数都会执行代码,javascript,asp.net-mvc,razor,simplemembership,Javascript,Asp.net Mvc,Razor,Simplemembership,我有一个问题,用户总是注销,即使功能注销尚未执行。这似乎是由于代码在c#中(这是在razor布局页面mvc4中) var idleTimer=0; 函数notIdle() { clearTimeout(idleTimer); idleTimer=setTimeout(函数(){logout()},5000); } 函数注销() { @如果(Request.IsAuthenticated) { WebSecurity.Logout(); } window.location.reload(); c
var idleTimer=0;
函数notIdle()
{
clearTimeout(idleTimer);
idleTimer=setTimeout(函数(){logout()},5000);
}
函数注销()
{
@如果(Request.IsAuthenticated)
{
WebSecurity.Logout();
}
window.location.reload();
clearTimeout(idleTimer);
}
如Ryan所述,加载页面时将运行C#代码。我可以建议创建一个控制器方法Logout
,将代码放在那里,然后在脚本Logout()
函数中重定向到该方法。代码如下:HTML
<button onclick="logout()">Logout</button>
C#
是的,当页面加载时,C代码将在服务器端运行。如果您想从javascript调用服务器端方法,您必须进行AJAX调用或其他操作。这不是我想要的,因为这段代码只是在用户空闲时注销用户。我意识到我可以检查aspxAuth cookie是否存在,以检查用户是否经过身份验证。
<button onclick="logout()">Logout</button>
//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.
}
public ActionResult Logout()
{
if (Request.IsAuthenticated)
{
WebSecurity.Logout();
}
return RedirectToAction("Index", "Home"); //redirect to your previous page
}