0
var ManagerAccess = context.Principal.FindFirst(ClaimTypes.System).Value;

Here I want to execute like this: (claimstypes.ManagerAccess).value;

Is this possible?

Lukas
  • 1,699
  • 1
  • 16
  • 49
sakinala
  • 11
  • 3
  • Can you provide some more information? Are you using JWT or ASP.NET Identity? Have you tried something that didn't work, and what was it? – Matt U Dec 02 '19 at 15:15
  • When and where do you want to add custom claims types?Besides,`Claimstypes` is static class you could not use `Claimstypes.ManagerAccess`, could you use `context.Principal.FindFirst("ManagerAccess").Value`? – Ryan Dec 03 '19 at 03:09
  • .AddJwtBearer(x => { x.Events = new JwtBearerEvents { OnTokenValidated = context =>{ ///this are standard format from claimtypes var name = int.Parse(context.Principal.FindFirst(ClaimTypes.Name).Value); ///but i want to add my custom claimtypes like manager access and an other types based on my requirments.. var managerAccess = context.Principal.FindFirst(ClaimTypes.managerAccess).Value; var claims = new System.Collections.Generic.List{ new Claim("demo1", demo1)}; var appIdentity = new ClaimsIdentity(claims); – sakinala Dec 04 '19 at 06:19

1 Answers1

0

To add custom claim type, you could use IClaimsTransformation

public class CustomClaimsTransformer : IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("ManagerAccess", "MyValue"));
        return Task.FromResult(principal);
    }
}

Register in startup.cs

services.AddScoped<IClaimsTransformation, CustomClaimsTransformer>();

Check you claims:

var ManagerAccess = context.Principal.FindFirst("ManagerAccess").Value;

For more solutions, refer to Implementing custom claim with extended MVC Core Identity user

Ryan
  • 19,118
  • 10
  • 37
  • 53