加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:20:25 所属栏目:asp.Net 来源:网络整理
导读:我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建一个ClaimIdentity.但是identity.Name不是searlized.如何让OpenIdConnectServer serarlize这个名字?谢谢. 上一个问题在这里是How to create a ClaimIdentity in asp.net 5 v
我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建一个ClaimIdentity.但是identity.Name不是searlized.如何让OpenIdConnectServer serarlize这个名字?谢谢.

上一个问题在这里是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));

解决方法

为避免泄露机密数据,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扩展:

identity.AddClaim(ClaimTypes.Name,"Pinpoint",OpenIdConnectConstants.Destinations.AccessToken,OpenIdConnectConstants.Destinations.IdentityToken);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读