gpt4 book ai didi

asp.net - 如何为现有ClaimsIdentity指定目的地?

转载 作者:行者123 更新时间:2023-12-04 22:58:35 25 4
gpt4 key购买 nike

我正在使用下面的代码在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com