我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建ClaimIdentity。但是identity.Name不会被标准化。如何让OpenIdConnectServer序列化名称?谢谢。
上一个问题在这里How to create a ClaimIdentity in asp.net 5
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));
最佳答案
为了避免泄露 secret 数据,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);
关于asp.net - 如何为现有ClaimsIdentity指定目的地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797838/