从 IdentityServer4 注销时撤销刷新令牌

时间:2021-04-09 18:18:07

标签: c# asp.net-core identityserver4

这是我的自定义注销端点:

agree() {
    return this.HttpClient.put(`${this.apiUrl}/agreeEula/`);
  }

它删除了 IdentityServer cookie,但如果用户此时拥有 refresh_token,他在注销后仍然可以使用它。如何撤销所有相关的刷新令牌?我尝试使用 [HttpPost("logout")] public async Task<IActionResult> LogoutAsync() { await _interaction.RevokeTokensForCurrentSessionAsync(); await HttpContext.SignOutAsync(); return Ok(); } ,但效果不如预期。

1 个答案:

答案 0 :(得分:1)

您的客户是 SPA 吗?如果是,请在注销操作时从 localstorage 中删除 refresh_token。

此外,您可以使用 HttpClient 在自定义注销函数中向此端点发出 POST 请求:

https://[Your_IdentityServer4_url]/connect/revocation
    token_type_hint=refresh_token
    client_id=[your_client_id]
    client_secret=[your_client_secret]
    token=[your_refresh_token]

或者,您可以像这样使用事件在退出时撤销刷新令牌。

.AddCookie("cookie", options =>
    {
        options.Cookie.Name = "mvccode";

        options.Events.OnSigningOut = async e =>
        {
            // revoke refresh token on sign-out
            await e.HttpContext.RevokeUserRefreshTokenAsync();
        };
    })

文档链接 - here