无法注销身份MVC 5应用程序

gog*_*gog 6 c# asp.net-mvc identity asp.net-mvc-5 asp.net-identity

我正在制作一个新的(空模板)ASP.NET MVC 5应用程序,我无法注销这个应用程序.我的注销行动:

public ActionResult LogOff()
{
    if (User.Identity.IsAuthenticated)
    {
        //break here
    }
    try
    {
        AuthenticationManager.SignOut();
        if (User.Identity.IsAuthenticated || Request.IsAuthenticated)
        {
            //break here;
        }
    }
    return RedirectToAction("Login", "Account");
}
Run Code Online (Sandbox Code Playgroud)

启动课程:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}
Run Code Online (Sandbox Code Playgroud)

应用背景:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext()
        : base("DefaultConnection", false)
    {
    }
 } 
Run Code Online (Sandbox Code Playgroud)

连接字符串:

<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=DataTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

操作LogOff()执行没有问题,并将我重定向到"登录"操作,但我仍然登录.它有什么问题?

Hor*_*Net 1

你的大部分代码对我来说似乎都不错。我猜你的操作方法有问题。通常这里唯一要做的就是

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

    return RedirectToAction("Login", "Account");
}
Run Code Online (Sandbox Code Playgroud)

我不知道 if 块对于您的注销过程是否至关重要,但这两行代码是您唯一需要做的事情。如果这很重要,您应该通过调试器检查SignOut方法是否被命中。

  • 另外,请记住,如果您使用 Chrome,它可能不会将您注销:http://stackoverflow.com/questions/23632725/how-do-i-log-a-user-out-when-they-关闭 asp-net-mvc/23633068#23633068 中的浏览器或选项卡 (2认同)