如何指定现有的ClaimsIdentity的目的地?

Ton*_*Woo 4 asp.net asp.net-identity openid-connect aspnet-contrib

我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建一个ClaimIdentity.但是identity.Name不是searlized.如何让OpenIdConnectServer serarlize这个名字?谢谢.

上一个问题在这里如何在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));       
Run Code Online (Sandbox Code Playgroud)

Kév*_*let 9

为避免泄露机密数据,请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);
Run Code Online (Sandbox Code Playgroud)

添加声明时,您还可以使用AddClaimdestinations参数的扩展程序:

identity.AddClaim(ClaimTypes.Name, "Pinpoint",
     OpenIdConnectConstants.Destinations.AccessToken,
     OpenIdConnectConstants.Destinations.IdentityToken);
Run Code Online (Sandbox Code Playgroud)