MVC AuthenticationManager.SignOut()未注销

Ram*_*iii 12 asp.net-mvc asp.net-identity

我的项目基于Visual Studio 2013的MVC 5项目模板(个人用户帐户选项).我一直依赖于我的用户的默认登录和注销方法.但我不确定我做了什么,在某些时候,用户不能再退出,但他们可以作为另一个用户登录.

这是帐户控制器的默认Logoff方法

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
        return RedirectToAction("Index", "Home");
    }
    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是显示用户用户名的默认_LoginPartial.cshtml视图.

    @using Microsoft.AspNet.Identity
    @if (Request.IsAuthenticated)
    {
        using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
        {
            @Html.AntiForgeryToken()

            <ul class="nav navbar-nav navbar-right">
                <li>
                    @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
                </li>
                <li><a    href="javascript:document.getElementById('logoutForm').submit()">Log off</a>            </li>
            </ul>
        }
    }
    else
    {
        <ul class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
            <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
        </ul>
    }
Run Code Online (Sandbox Code Playgroud)

当用户注销时,它会将用户引导至登录页面,但仍会显示用户的用户名,这意味着他们尚未注销.并且浏览器上的URL显示 http:// localhost/Account/Login?ReturnUrl =%2FAccount%2FLogOff

它不会将用户带回Home的Index页面.所以我的猜测是在语句AuthenticationManager.SignOut();中发生了一些事情.我很困惑,因为我没有改变任何帐户控制器.

任何领导都将非常感激.

小智 9

我有同样的问题.在CodePlex上查看此问题:

http://web.archive.org/web/20160403071605/https://aspnetidentity.codeplex.com/workitem/2347

尝试更换AuthenticationManager.SignOut()AuthenticationManager.Signout(DefaultAuthenticationTypes.ApplicationCookie);

我希望我帮助你.:-)

  • 我的问题实际上并没有发生在`SignOut()`.这是因为我在控制器上方添加了`[authorize(Role ="admin")]`我没有意识到.但我相信它会帮助许多人在使用Owin身份验证的`SignOut()`中出现问题. (2认同)

Ice*_*urn 8

只需在 SignOut() 之后添加这行代码:

 HttpContext.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
Run Code Online (Sandbox Code Playgroud)

还要检查一下: Page.User.Identity.IsAuthenticated 在 FormsAuthentication.SignOut() 之后仍然为真