Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNet Core - Settings `NameClaimType` and `RoleClaimType`

How do I change the Identity values NameClaimType and RoleClaimType to "sub" and "role" instead the default SOAP URLs below:

NameClaimType and RoleClaimType (More context on the motivation on this github issue: https://github.com/GestionSystemesTelecom/fake-authentication-jwtbearer/issues/4)

like image 651
Lucas Lobosque Avatar asked Oct 20 '25 06:10

Lucas Lobosque


1 Answers

  1. Configure your identity
services.AddDefaultIdentity<IIdentityUser>(options =>
{ 
    options.ClaimsIdentity.UserNameClaimType = "sub";
    options.ClaimsIdentity.RoleClaimType = "role";
})
  1. Configure your JWT token
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
     .AddJwtBearer(options =>
     {
           options.TokenValidationParameters.NameClaimType = "sub";
           options.TokenValidationParameters.RoleClaimType = "role";
     });
  1. Use await CreateAsync(user) method on IUserClaimsPrincipalFactory<IIdentityUser> instance. It will create correct ClaimsIdentity object with user claims that you can use during JWT token generation.
var principal = await _userClaimsPrincipalFactory.CreateAsync(user);
var token = new JwtSecurityToken(issuer, audience, principal.Claims, notBefore, expires, credentials);
like image 151
Boris Maslennikov Avatar answered Oct 24 '25 08:10

Boris Maslennikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!