【问题标题】:How to log out External Authentication Schemes like Facebook in ASP .NET MVC Core?如何在 ASP .NET MVC Core 中注销像 Facebook 这样的外部身份验证方案?
【发布时间】:2017-12-28 17:21:12
【问题描述】:

我有一个 ASP .NET MVC Core 应用程序,我正在实现一个非常简单的登录页面,我使用 IdentityUser 让用户使用用户名、电子邮件和密码登录。我也在使用外部身份验证提供程序,例如 Facebook。正常登录/注销正在正常工作。当涉及到 facebook 时,登录工作正常:

 public IActionResult FacebookLogin()
    {
        AuthenticationProperties authProps = new AuthenticationProperties
        {
            RedirectUri = Url.Action("Index", "Home")
        };

        return Challenge(authProps, "Facebook");
    }

但是当我使用 Facebook 登录时,我无法注销。这是我的注销代码:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Logout()
    {
        await _signinManager.SignOutAsync();
        return RedirectToAction("Index", "Home");
    }

无论我做什么,当我使用 Facebook 登录时,我都无法退出 任何帮助表示赞赏。 值得注意的是,我的部分观点如下:

每当我使用 facebook 登录时,IsAuthenticated 标志都会设置为 true,但当我注销时,它永远不会设置为 false

【问题讨论】:

  • 那么,当您尝试注销时,您会收到错误消息吗?你有什么事情要做吗?
  • @R.Richards 不,我没有收到任何错误。我有一个部分视图,我使用 User.Identity.IsAuthenticated 在注销或登录按钮之间进行选择。当我尝试注销 Facebook 时,它永远不会设置为 false。
  • This 可能会提供一个或两个解决方法。似乎是一个已知问题。
  • @R.Richards 是的,我在您分享的帖子中选择了最后一个答案,它得到了修复。非常感谢!

标签: c# authentication oauth asp.net-core facebook-login


【解决方案1】:

感谢@R.Richards 分享旧帖。可以看here

我选择在我的注销操作方法中使用以下代码:

Response.Cookies.Delete(".AspNetCore.<nameofcookie>"); 

在我的 Startup.cs 和 Configure 方法中:

 app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = "ApplicationCookie",
            AutomaticAuthenticate = true
        });

        app.UseFacebookAuthentication(new FacebookOptions
        {
            AuthenticationScheme = "Facebook",
            AppId = "App_ID",
            AppSecret = "Secret_Id",
            SignInScheme = "ApplicationCookie",
            AutomaticAuthenticate = true
        });

在我的 AccountController 中的 Logout 操作方法中:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Logout()
    {
        await _signinManager.SignOutAsync();
        Response.Cookies.Delete(".AspNetCore.ApplicationCookie");
        return RedirectToAction("Index", "Home");
    }

【讨论】:

  • 这对 AspNetCore 2.0 是否仍然有效。名称如何配置“.AspNetCore.ApplicationCookie”,它“神奇”地设置在哪里?我们可以改变吗?
  • @YvesSchelpe 我不得不返回与这篇文章相关的项目并将其升级到 AspNetCore 2.0 才能回答你。首先,现在的设置不同了。 They have deprecated the previous setup 对于“.AspNetCore.ApplicationCookie”字符串,这是因为在我在 Startup.cs 文件中的设置中,我有:app.UseCookieAuthentication(new CookieAuthenticationOptions {AuthenticationScheme = "ApplicationCookie"}); 我只是给它加上了前缀。我不确定是否可以更改 .AspNetCore 部分,但其余部分键应该可以更改
猜你喜欢
  • 2018-03-23
  • 2018-12-17
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2010-10-31
  • 1970-01-01
  • 2021-06-15
相关资源
最近更新 更多