IdentiyServer4 GrantValidationResult的响应有所不同

时间:2018-10-28 21:27:49

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

我有一个Custom Grant Validator,用于令牌翻译。 JWT的参考令牌。

public class ExchangeReferenceTokenGrantValidator : IExtensionGrantValidator
{
    private readonly ITokenValidator _validator;

    public ExchangeReferenceTokenGrantValidator(ITokenValidator validator)
    {
        _validator = validator;
    }

    public async Task ValidateAsync(ExtensionGrantValidationContext context)
    {
        var referenceToken = context.Request.Raw.Get("token");
        if (referenceToken == null)
        {
            context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Missing Reference Token");
            return;
        }

        var token = await _validator.ValidateAccessTokenAsync(referenceToken);
        if (token == null || token.IsError)
        {
            context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Invalid Reference Token");
            return;
        }

        var subjectId = token.Claims.FirstOrDefault(x => x.Type == "sub");

        // Generate the JWT as if it was for the reference token's client
        context.Request.Client = token.Client;
        context.Request.Client.AccessTokenType = AccessTokenType.Jwt;
        context.Request.Client.AlwaysSendClientClaims = true;
        context.Request.Client.AccessTokenLifetime = 1800;
        context.Result = new GrantValidationResult(subjectId.Value, GrantType);
    }

    public string GrantType => "exchange_reference_token";
}

但是,当我第一次调用此端点时,返回了一个参考令牌,这很奇怪,因为我将AccessTokenType设置为JWT。第二次调用端点时,将生成一个JWT。我在这里想念什么?为什么响应不同?我应该总是得到一个JWT。

0 个答案:

没有答案