【问题标题】:How to revoke refresh token in aspnet core which is store in Identiy Server Database如何撤销存储在 Identity Server 数据库中的 asp net core 中的刷新令牌
【发布时间】:2020-08-28 07:22:02
【问题描述】:

通过从 Aspnet 核心(API 层)调用身份服务器 4 的 RevokeAccessTokenAsync(param: accesstoken) 方法来轻松撤销令牌。 我可以轻松地获取登录用户的访问令牌,因为我可以从 HttpContext.GetTokenAsync("accesstoken") 获取它。但是,此方法不适用于刷新令牌,因此我无法禁用用户再次获取访问令牌。 刷新令牌存储在我不想访问的身份服务器数据库中,因为我想从 API 层调用撤销访问令牌。

这让我获得了访问令牌: 访问令牌 = 等待 HttpContext.GetTokenAsync("access_token");

这将返回一个空字符串: refreshtoken = 等待 HttpContext.GetTokenAsync("refresh_token");

【问题讨论】:

  • 嗨,Ayan,欢迎来到社区!请发布您尝试过的内容以及针对您的场景的更多详细信息。由于编写了这个问题,因此期望得到好的答案仍然有点过于宽泛。请添加您已经尝试过的内容,以及您不能或不想访问身份数据库的原因,这可能是您撤销用户访问权限的最佳选择。

标签: asp.net-core identityserver4 refresh-token


【解决方案1】:

尝试了一些可以解决我的问题的方法。覆盖 RevokeRefreshTokenAsync 并假设传递的令牌是访问令牌,现在使用访问令牌撤消所有刷新令牌。

         public class CustomTokenRevocationResponseGenerator : TokenRevocationResponseGenerator, ITokenRevocationResponseGenerator
{

    /// <summary>
    /// Revoke refresh token only if it belongs to client doing the request
    /// </summary>
    protected override async Task<bool> RevokeRefreshTokenAsync(TokenRevocationRequestValidationResult validationResult)
    {
        Logger.LogInformation("Revoking refresh token");
        //assume the token is access token instead of refresh token
        var token = await ReferenceTokenStore.GetReferenceTokenAsync(validationResult.Token);
        if (token != null)
        {
            Logger.LogInformation($"Revoking refresh token for clientId {token.ClientId} ");
            if (token.ClientId == validationResult.Client.ClientId)
            {
                Logger.LogDebug("Refresh token revoked");
                await RefreshTokenStore.RemoveRefreshTokensAsync(token.SubjectId, token.ClientId);
                await ReferenceTokenStore.RemoveReferenceTokensAsync(token.SubjectId, token.ClientId);
            }
            else
            {
                Logger.LogWarning("Client {clientId} tried to revoke a refresh token belonging to a different client: {clientId}", validationResult.Client.ClientId, token.ClientId);
            }

            return true;
        }

        return false;
    }



    public CustomTokenRevocationResponseGenerator(IReferenceTokenStore referenceTokenStore, IRefreshTokenStore refreshTokenStore, ILogger<TokenRevocationResponseGenerator> logger) : base(referenceTokenStore, refreshTokenStore, logger)
    {
    }
}

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2020-02-20
    • 2016-06-16
    • 2021-09-24
    • 2019-04-12
    • 2021-11-05
    • 2018-06-01
    • 2015-12-13
    相关资源
    最近更新 更多