javascript 如何在asp.net mvc中注销当前用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13927818/
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-10-26 20:12:16  来源:igfitidea点击:

how to log out the current user in asp.net mvc

javascriptjqueryasp.net-mvc-3jquery-ui

提问by Jonathan

I have used the below link to end a session.

我已使用以下链接结束会话。

http://pure-essence.net/2010/02/14/jquery-session-timeout-countdown/

http://pure-essence.net/2010/02/14/jquery-session-timeout-countdown/

In that i am able to redirect to the login page. But the user is not logging off. Just it is redirecting to that page. But it is not logging out. How can i log out the session in that case. Any help is appreciated.

这样我就可以重定向到登录页面。但用户并未注销。只是它正在重定向到该页面。但它没有注销。在这种情况下我如何注销会话。任何帮助表示赞赏。

回答by Yasser Shaikh

Short Answer : Use FormsAuthentication.SignOut();

简答:使用 FormsAuthentication.SignOut();

Long Answer

长答案

public ActionResult LogOff()
{
    FormsAuthentication.SignOut();

    return RedirectToAction("Login", "Account");
}

private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

Use [Authorize]above the actions which you don't want the user to do after logging out. This you can do by adding this attribute above actions individually or marking the entire class with [Authorize]so that all methods can now be only be accessed by authenticated users.

[Authorize]上面使用您不希望用户在注销后执行的操作。您可以通过在操作上方单独添加此属性或标记整个类来实现[Authorize]这一点,以便所有方法现在只能由经过身份验证的用户访问。

Hope this helps.

希望这可以帮助。

回答by Nishān Wickramrathna

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

This should work in newer versions.

这应该适用于较新的版本。