【问题标题】:Cannot logoff of identity MVC 5 application无法注销身份 MVC 5 应用程序
【发布时间】:2014-08-01 19:18:38
【问题描述】:

我正在制作一个新的(空模板)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");
}

启动类:

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);
    }
}

应用程序上下文:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext()
        : base("DefaultConnection", false)
    {
    }
 } 

连接字符串:

<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=DataTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

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

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 asp.net-identity identity


    【解决方案1】:

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

    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
    
        return RedirectToAction("Login", "Account");
    }
    

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

    【讨论】:

    • 是的,我知道,我只是放了其他代码来检查用户是否仍然经过身份验证。问题是应用程序似乎从 cookie 或其他东西中获取数据,然后我在执行此操作后仍然登录。
    • 另外,请记住,如果您使用的是 Chrome,它可能不会将您注销:stackoverflow.com/questions/23632725/…
    • 在执行此方法后应删除 cookie(在您的启动类中选择设置)。如果 cookie 仍然存在,请检查浏览器的开发人员工具。关于 Chrome,我经常使用它进行开发,并且在使用 Identity 时还没有遇到问题。
    • @George 我也有同样的问题,当我用 IE 测试它时,问题就消失了。 Chrome 没有注销我。
    【解决方案2】:

    试试这个:

    [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            //AuthenticationManager.SignOut();
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
            Session.Abandon();
            return RedirectToAction("Login", "Account");
        }
    

    【讨论】:

    • 为什么是这个帖子?
    • 对不起,为什么要使用[HttpPost]。应该更具体
    • @Halter - 改变状态的操作不应该是 Get 操作。这使得某人很容易因为恶作剧而退出登录,因为有人添加了一个 &lt;img&gt; 标签,例如,它指向 /Logoff。
    【解决方案3】:

    这对我有用: 在您的 RouteConfig.cs 中创建一个路由,例如

     routes.MapRoute(
           "userlogout",
           "Account/Logout",
           new { controller = "Account", action = "LogOff" }
           );
    

    并且您可以在 AccountController.cs 或添加其他人建议的添加(如session.abandon(); 等) 但就像下面应该工作

    [HttpPost] 
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
    
        return RedirectToAction("Login", "Account");
    }
    

    【讨论】:

      【解决方案4】:

      这似乎对我很有效。

      public ActionResult Logoff()
      {
          HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
          HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
          HttpContext.Response.Cache.SetNoStore();
      
          Session.Clear();
          Session.Abandon();
          Session.RemoveAll();
          FormsAuthentication.SignOut();
          return RedirectToAction("Index", "Home");
      }
      

      【讨论】:

        【解决方案5】:
        app.UseCookieAuthentication(new CookieAuthenticationOptions
                    {
                        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                        LoginPath = new PathString("/Account/Login"),
                        LogoutPath = new PathString("/Account/SignOut"),
                        Provider = new CookieAuthenticationProvider
                        {
                            // Enables the application to validate the security stamp when the user logs in.
                            // This is a security feature which is used when you change a password or add an external login to your account.  
                            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                                validateInterval: TimeSpan.FromMinutes(30),
                                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                        }
                    });      
        

        ^^将 Startup.Auth.cs 中的“LogoutPath”设置为您想要的任何路径

        【讨论】:

          【解决方案6】:

          在这种情况下,您还可以执行以下操作: 从 LogOff 操作中删除 [HttpPost] 并改为使用 [HttpGet]。 您只需要传递 AntiForgeryToken。但问题是这是否是一种非常安全的方式。更多信息在这里:Using MVC3's AntiForgeryToken in HTTP GET to avoid Javascript CSRF vulnerability

          [HttpGet] 
          [ValidateAntiForgeryToken]
          public ActionResult LogOff()
          {
               AuthenticationManager.SignOut();
               return RedirectToAction("Login", "Account");
          }
          

          【讨论】:

            【解决方案7】:

            关于 ASP .Net MVC 注销不起作用:-

            我遇到了一个问题,即在生产模式下托管在 IIS 上的应用程序无法与 chrome 一起正常工作

            虽然它在所有浏览器中使用 Visual Studio Dev 托管 - 在 IE 上的生产模式下工作正常

            我在 Startup.Auth.CS 中遇到问题。确保以下内容不存在重复配置

            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.UseCookieAuthentication((new CookieAuthenticationOptions(.....))
            

            【讨论】:

              猜你喜欢
              • 2015-05-31
              • 2014-03-03
              • 2023-04-03
              • 1970-01-01
              • 2016-06-15
              • 2016-09-03
              • 1970-01-01
              • 2018-07-22
              • 2019-03-01
              相关资源
              最近更新 更多