我正在使用 JWT 令牌和 OpenIdConnectServer。一切都很好,但我无法在令牌响应中添加自定义属性......结果如下:
resource": "resource_server_1",
"token_type": "bearer",
"access_token": "eyJhb....LSk5PQldEVVFaTllNU",
"expires_in": "3600"
我想添加一些属性,例如用户名或角色...我正在尝试通过 AuthenticationProperties 添加,但它不起作用。这是我的代码:
public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context)
{
ClaimsIdentity identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
identity.AddClaim(ClaimTypes.Name, "test", "token id_token");
identity.AddClaim(ClaimTypes.Role, "test", "token id_token");
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(new Dictionary<string, string>
{
{"username", "test" }
}),
context.Options.AuthenticationScheme);
ticket.SetResources(new[] { "resource_server_1" });
context.Validated(ticket);
return Task.FromResult<object>(null);
}