使用 OpenIddict 请求令牌时,如何添加要返回的自定义声明?
How can I add custom claims to be returned when requesting a token using OpenIddict?
我正在构建 ASP.NET Core 1.1 应用程序(跨平台)并尝试(使用 this sample)在请求时向 returned access_token
添加自定义声明/connect/token
端点。
我需要的不仅是 return 在 access_token
中序列化的声明,而且 return 他们在这样的响应中:
{
"token_type": "Bearer",
"access_token": "...",
"expires_in": 1799,
"custom_claim": "..."
}
我在 Internet 上发现我必须使用 AspNet.Security.OpenIdConnect.Server
并编写我的提供商才能执行我想要的操作。
没有使用第一个示例的简单方法吗?
我使用的是 OAUth 2.0,授权类型 Password
,没有 JWT。
不是不使用 JWT 的要求,只是我在 ASP.NET 4.5
中习惯了 OAuth
好吧,我们通过使用 Configure 中 OpenIdConnectOptions 的 Events 属性 来做到这一点 方法 Startup class 添加 Open Id Connect 中间件时,例如:
Events = new OpenIdConnectEvents
{
OnTicketReceived = n =>
{
//TODO Your logic here to add custom claims via n.Principal.Identities.First().AddClaims();
return Task.CompletedTask;
}
}
这是您的用例的选项吗?
What I need is to not only return the claims serialized in the access_token but to return them in the response like this:
虽然我鼓励您将这些声明存储在身份令牌中 - 以便客户端可以以完全标准的方式轻松读取它们,但在 OpenIddict 1.0 和 2.0 RTM 中是可能的。为此,您有 2 个选择:
使用特殊的 "public" 属性(在您的授权控制器中,创建身份验证票证的地方):
ticket.SetProperty("custom_claim" + OpenIddictConstants.PropertyTypes.String, user.Id);
注意:OpenIddictConstants.PropertyTypes.String
是一个特殊后缀,表示添加到票证的身份验证 属性 可以作为令牌响应的一部分公开。如果您希望以 JSON 数字或更复杂的 JSON 结构返回声明,则可以使用其他常量。
使用事件模型(在 Startup.cs 中):
services.AddOpenIddict()
// Register the OpenIddict core services.
.AddCore(options =>
{
// ...
})
// Register the OpenIddict server handler.
.AddServer(options =>
{
// ...
options.AddEventHandler<OpenIddictServerEvents.ApplyTokenResponse>(
notification =>
{
if (string.IsNullOrEmpty(notification.Context.Error))
{
var principal = notification.Context.Ticket.Principal;
var response = notification.Context.Response;
response["custom_claim"] = principal.FindFirst("your_claim_attached_to_the_principal").Value;
}
return Task.FromResult(OpenIddictServerEventState.Unhandled);
});
})
// Register the OpenIddict validation handler.
.AddValidation();
@Pinpoint 在他的 openiddict-samples
I followed this article 存储库中的回答(在 Implementing the Connect/Token Endpoint
部分)..
我从他的回答中发现我正在尝试做的事情不是标准的,这就是为什么它不是那么明显和容易做的原因。
您需要使用 JWT 并向其添加自定义声明,以便客户端可以对其进行解码并获取声明,而不是通过它自己的响应发送它们。
我正在构建 ASP.NET Core 1.1 应用程序(跨平台)并尝试(使用 this sample)在请求时向 returned access_token
添加自定义声明/connect/token
端点。
我需要的不仅是 return 在 access_token
中序列化的声明,而且 return 他们在这样的响应中:
{
"token_type": "Bearer",
"access_token": "...",
"expires_in": 1799,
"custom_claim": "..."
}
我在 Internet 上发现我必须使用 AspNet.Security.OpenIdConnect.Server
并编写我的提供商才能执行我想要的操作。
没有使用第一个示例的简单方法吗?
我使用的是 OAUth 2.0,授权类型 Password
,没有 JWT。
不是不使用 JWT 的要求,只是我在 ASP.NET 4.5
好吧,我们通过使用 Configure 中 OpenIdConnectOptions 的 Events 属性 来做到这一点 方法 Startup class 添加 Open Id Connect 中间件时,例如:
Events = new OpenIdConnectEvents
{
OnTicketReceived = n =>
{
//TODO Your logic here to add custom claims via n.Principal.Identities.First().AddClaims();
return Task.CompletedTask;
}
}
这是您的用例的选项吗?
What I need is to not only return the claims serialized in the access_token but to return them in the response like this:
虽然我鼓励您将这些声明存储在身份令牌中 - 以便客户端可以以完全标准的方式轻松读取它们,但在 OpenIddict 1.0 和 2.0 RTM 中是可能的。为此,您有 2 个选择:
使用特殊的 "public" 属性(在您的授权控制器中,创建身份验证票证的地方):
ticket.SetProperty("custom_claim" + OpenIddictConstants.PropertyTypes.String, user.Id);
注意:OpenIddictConstants.PropertyTypes.String
是一个特殊后缀,表示添加到票证的身份验证 属性 可以作为令牌响应的一部分公开。如果您希望以 JSON 数字或更复杂的 JSON 结构返回声明,则可以使用其他常量。
使用事件模型(在 Startup.cs 中):
services.AddOpenIddict()
// Register the OpenIddict core services.
.AddCore(options =>
{
// ...
})
// Register the OpenIddict server handler.
.AddServer(options =>
{
// ...
options.AddEventHandler<OpenIddictServerEvents.ApplyTokenResponse>(
notification =>
{
if (string.IsNullOrEmpty(notification.Context.Error))
{
var principal = notification.Context.Ticket.Principal;
var response = notification.Context.Response;
response["custom_claim"] = principal.FindFirst("your_claim_attached_to_the_principal").Value;
}
return Task.FromResult(OpenIddictServerEventState.Unhandled);
});
})
// Register the OpenIddict validation handler.
.AddValidation();
@Pinpoint 在他的 openiddict-samples
I followed this article 存储库中的回答(在 Implementing the Connect/Token Endpoint
部分)..
我从他的回答中发现我正在尝试做的事情不是标准的,这就是为什么它不是那么明显和容易做的原因。
您需要使用 JWT 并向其添加自定义声明,以便客户端可以对其进行解码并获取声明,而不是通过它自己的响应发送它们。