How do I customize /connect/token to take custom parameters?(如何定制/连接/令牌以采用定制参数?)
本文介绍了如何定制/连接/令牌以采用定制参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要通过客户端凭据流获取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;
}
这篇关于如何定制/连接/令牌以采用定制参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何定制/连接/令牌以采用定制参数?
基础教程推荐
猜你喜欢
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
