如何定制/连接/令牌以采用定制参数? [英] How do I customize /connect/token to take custom parameters?

查看:29
本文介绍了如何定制/连接/令牌以采用定制参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要通过客户端凭据流获取Access_Token,请调用/Connect/Token,如下所示

curl -X POST https://<identityserver>/connect/token 
-H 'Content-Type: application/x-www-form-urlencoded' 
-d 'grant_type=client_credentials&scope=myscope1'

我似乎不能自定义/连接/令牌来接受自定义参数?我想从API中获取自定义值,并通过ICustomTokenRequestValidator(https://stackoverflow.com/a/43930786/103264)将它们添加到我的自定义声明中

推荐答案

您可以使用acr_values参数自定义您的调用。例如,下面我们将tenantId值作为参数,对其进行验证并将其作为声明放入令牌中。 该调用如下所示:

curl -d "grant_type=client_credentials&client_id=the-svc-client&client_secret=the-secret&acr_values=tenant:DevStable" https://login.my.site/connect/token

和vlidator(部分复制并粘贴自AuthRequestValidator):

public Task ValidateAsync(CustomTokenRequestValidationContext context)
{
    var request = context.Result.ValidatedRequest;

    if (request.GrantType == OidcConstants.GrantTypes.ClientCredentials)
    {
        //////////////////////////////////////////////////////////
        // check acr_values
        //////////////////////////////////////////////////////////
        var acrValues = request.Raw.Get(OidcConstants.AuthorizeRequest.AcrValues);
        if (acrValues.IsPresent())
        {
            if (acrValues.Length > context.Result.ValidatedRequest.Options
                    .InputLengthRestrictions.AcrValues)
            {
                _logger.LogError("Acr values too long", request);
                context.Result.Error = "Acr values too long";
                context.Result.ErrorDescription = "Invalid acr_values";
                context.Result.IsError = true;
                return Task.CompletedTask;
            }

            var acr = acrValues.FromSpaceSeparatedString().Distinct().ToList();


            //////////////////////////////////////////////////////////
            // check custom acr_values: tenant
            //////////////////////////////////////////////////////////

            string tenant = acr.FirstOrDefault(x => x.StartsWith(nameof(tenant)));

            tenant = tenant?.Substring(nameof(tenant).Length+1);

            if (!tenant.IsNullOrEmpty())
            {
                var tenantInfo = _tenantService.GetTenantInfoAsync(tenant).Result;
                // if tenant is present in request but not in the list, raise error!
                if (tenantInfo == null)
                {
                    _logger.LogError("Requested tenant not found", request);
                    context.Result.Error = "Requested tenant not found";
                    context.Result.ErrorDescription = "Invalid tenant";
                    context.Result.IsError = true;
                }

                context.Result.ValidatedRequest.ClientClaims.Add(
                    new Claim(Constants.TenantIdClaimType, tenant));            
            }
        }
    }
    return Task.CompletedTask;
}

这篇关于如何定制/连接/令牌以采用定制参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆