我使用以下代码在OpenIdConnectServerProvider.AuthorizationProvider中创建ClaimIdentity。名称未初始化。如何允许OpenIdConnectServer序列化名称?谢谢
上一个问题是如何在asp.net 5 中创建ClaimIdentity
var user = await userManager.FindByNameAsync(context.UserName);
var factory = context.HttpContext.RequestServices.GetRequiredService<IUserClaimsPrincipalFactory<ApplicationUser>>();
var identity = await factory.CreateAsync(user);
context.Validated(new ClaimsPrincipal(identity));
为了避免泄露机密数据,AspNet.Security.OpenIdConnect.Server
拒绝序列化未明确指定目标的声明。
要序列化名称(或任何其他声明),可以使用.SetDestinations
扩展名:
var principal = await factory.CreateAsync(user);
var name = principal.FindFirst(ClaimTypes.Name);
if (name != null) {
// Use "id_token" to serialize the claim in the identity token or "access_token"
// to serialize it in the access token. You can also specify both destinations.
name.SetDestinations(OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
}
context.Validate(principal);
添加索赔时,您还可以使用AddClaim
扩展,其中包含destinations
参数:
identity.AddClaim(ClaimTypes.Name, "Pinpoint",
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);