I'm using below code to create a ClaimIdentity in OpenIdConnectServerProvider.AuthorizationProvider. But the identity.Name is not searlized. How to allow the OpenIdConnectServer serarlize the name? Thanks.
The previous question is here 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));
By default, a user's claims are stored in the authentication cookie.
In . NET Core, the ClaimsIdentity class represents a user in your application. It helps describe who they are and helps manage the list of claims which describe what they can do.
Claim based authorization checks are declarative - the developer embeds them within their code, against a controller or an action within a controller, specifying claims which the current user must possess, and optionally the value the claim must hold to access the requested resource.
To avoid leaking confidential data, AspNet.Security.OpenIdConnect.Server
refuses to serialize the claims that don't explicitly specify a destination.
To serialize the name (or any other claim), you can use the .SetDestinations
extension:
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);
When adding a claim, you can also use the AddClaim
extension taking a destinations
parameter:
identity.AddClaim(ClaimTypes.Name, "Pinpoint",
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With