ASP.Net MVC 5带有身份验证2.2.0,注销功能无法正常工作

36

我在一个测试ASP.Net MVC 5网站上使用基本的登录(用于互联网网站)。

登录功能正常,但是当我尝试注销时它却没有发生作用。注销链接确实调用了以下控制器操作:

public ActionResult LogOff()
{
    AuthenticationManager.SignOut();
    return RedirectToAction("Index", "Home");
}

但是用户仍然保持登录状态。我该如何确保用户实际上已经注销了?


可以参考这个链接:http://stackoverflow.com/questions/28263095/logout-functionality-not-working-with-asp-net-identity - janhartmann
3个回答

50

我之前遇到过这个问题,需要更改:

AuthenticationManager.SignOut();

To:
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

假设您正在使用ApplicationCookie来存储登录信息。


2
谢谢 - 对我有用。小提示:应该是AuthenticationManager(根据自动生成的代码),而不是Authentication - Krishna Gupta
实际上它并没有从服务器端注销。https://dev59.com/LmAf5IYBdhLWcg3wgzHI - Jeeva J
@JeevaJsb 那个问题是关于Bearer令牌的,这与Cookies完全不同。 - Ashley Medway

4
更好的方式:
public ActionResult Logout()
{
    SignInManager.AuthenticationManager.SignOut();
    return RedirectToAction("Index", "support", new { area = "" });
}

或者您可以像这样将注入的 SignInManager 用于控制器:

public ActionResult Logout()
{
    _signInManager.AuthenticationManager.SignOut();
    return RedirectToAction("Index", "support", new { area = "" });
}

没有区别。


-4

我曾经遇到过无法注销的同样问题。我一直保持登录状态,只能重定向回主页视图。我使用Chrome浏览器尝试了一下,然后在Firefox和IE中也尝试了一下,但没有出现这个问题。然后我在Chrome中清除了我的Cookie,所有问题都得到了解决。如果其他人遇到这个问题,这可能是一个快速简单的第一步。


2
我们在将应用程序从AspNet.Identity 2.0.1升级到2.2.0后遇到了相同的问题。清除cookie确实会注销用户,但这不是一个非常实际的解决方案。我们采用了被接受的答案中的修复方法,并解决了这个问题。 - Avalanchis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接