如何在 ASP .NET MVC Core 中注销 Facebook 等外部身份验证方案?

How to log out External Authentication Schemes like Facebook in ASP .NET MVC Core?

我有一个 ASP .NET MVC Core 应用程序,我正在实现一个非常简单的登录页面,我在其中使用 IdentityUser 让用户使用用户名、电子邮件和密码登录。我也在使用外部身份验证提供程序,例如 Facebook。正常 login/logout 正常工作。当涉及到 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 在旧 post 上的分享。可以看看here

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

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

在我的 Startup.csConfigure 方法中:

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

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

并且在我的 AccountControllerLogout 操作方法中:

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