我正在尝试配置我的 Duende(以前称为 Identity Server4)身份服务器以进行身份验证和授权。对于身份验证部分,我使用外部身份验证服务,结果之一是UserID。然后,我想将此 UserID 添加为我的访问令牌中的自定义声明。但是,我不知道这是如何完成的。
具体来说,我想实现这样的事情:
// Client/program.cs
var client = new HttpClient();
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = https://localhost:5001/connect/token,
ClientId = "1",
ClientSecret = "secret",
Scope = "api1",
UserID = UserID // here is the problem. It creates the correct access token without this line
});
问题是 UserID 未定义为 RequestClientCredentialsTokenAsync 的一部分。
有什么办法可以添加吗?
提前谢谢您。
ClientCredentials Flow 不涉及任何用户交互,因为不会有任何登录用户相关数据。
您可以使用传统的 ResourceOwnerPassword Flow,它使用用户名和密码进行身份验证。您当前的方法与服务器到服务器的交互有关。
您将无法使用客户端凭据发送任何基于用户的参数。
您可以使用 IExtensionGrantValidator 接口为您的场景创建自定义授权。
您可以使用 RequestTokenAsync 发送附加参数。
var client = new HttpClient();
var response = await client.RequestTokenAsync(new TokenRequest
{
Address = "https://identityUrl/connect/token",
GrantType = "customgrant",
ClientId = "clientid",
ClientSecret = "secret",
Parameters =
{
{ "customParameter", "custom value"},
{ "scope", "api1" }
}
});
查看此文档了解更多详细信息 Identity Server 中的扩展授权