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;
}
这篇关于如何定制/连接/令牌以采用定制参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:如何定制/连接/令牌以采用定制参数?
基础教程推荐
猜你喜欢
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
