首页 文章

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

提问于
浏览
2

我有一个ASP .NET MVC核心应用程序,我正在实现一个非常简单的登录页面,我使用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登录时都无法注销任何帮助 . 值得注意的是,我的部分观点如下:

enter image description here

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

1 回答

  • 0

    感谢@ R.Richards分享旧帖子 . 你可以看到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
            });
    

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

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

相关问题