asp.net-mvc - MVC AuthenticationManager.SignOut() 未注销

标签 asp.net-mvc asp.net-identity

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

这是帐户 Controller 的默认注销方法

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

这是显示用户用户名的默认 _LoginPartial.cshtml View 。

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

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

它不会将用户带回主页的索引页面。所以我的猜测是在 AuthenticationManager.SignOut(); 语句中发生了一些事情。我很困惑,因为我没有对帐户 Controller 进行任何更改。

任何线索将不胜感激。

最佳答案

我也遇到了同样的问题。 在 CodePlex 上检查此问题:

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

尝试将 AuthenticationManager.SignOut() 替换为 AuthenticationManager.Signout(DefaultAuthenticationTypes.ApplicationCookie);

希望对你有帮助。 :-)

关于asp.net-mvc - MVC AuthenticationManager.SignOut() 未注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090808/

相关文章:

asp.net-mvc - 资源被解释为文档,但使用 MIME 类型应用程序/zip : 传输

asp.net-mvc - 以编程方式获取 MVC View 列表?

c# - 存储和检索在使用 facebook 或 twitter 授权后为用户收到的 token

asp.net-mvc - 用户不担任角色时的ASP.NET登录重定向循环

c# - 使用 html.beginform,如何将 {id} 的值添加到 post url 中?

asp.net-mvc - 如何在mvc中的textarea中预览html

asp.net-mvc - 添加更多 65 列时数据表出错?

authentication - 将 azure AD 身份验证与 asp.net core 身份个人帐户集成

c# - ASP.NET Identity userid 是顺序 guid

c# - 自定义类Identity/Entity Framework多对多关系