IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

How to get claims only via userinfo endpoint and not in the access token

Open eld1887 opened this issue 8 years ago • 2 comments

hi, i want to get custom claims via the userinfo endpoint. That works too but all of the custom claims are in access token too. I understand it in that way, that there is a possibility where i can put one or two claims (like email or name) in the access token and all of the other claims (given_name,...) are accessed via the userinfo endpoint. (and they are not in the access token)

my ProfileService is looking like that:

public class ProfileService : IProfileService
    {
        private readonly IUserClaimsPrincipalFactory<CustomUser> _claimsFactory;
        private readonly UserManager<CustomUser> _userManager;

        public ProfileService(UserManager<CustomUser> userManager,    IUserClaimsPrincipalFactory<CustomUser> claimsFactory)
        {
            _claimsFactory = claimsFactory;
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            var principal = await _claimsFactory.CreateAsync(user);

            var claims = principal.Claims.ToList();
            claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();

            claims.Add(new Claim(JwtClaimTypes.GivenName, user.FirstName));

            context.IssuedClaims = claims;

        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            context.IsActive = user != null;
        }
    }

this is my config.cs file:

return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email(),
            };
        }

 public static IEnumerable<ApiResource> GetApiResources()
      {
          return new List<ApiResource>
          {
              new ApiResource("api1", "My API", new [] {JwtClaimTypes.Name })
          };
      }
new Client
                {
                    ClientId = "xxx",
                    ClientName = "xxx",
                    //AccessTokenType = AccessTokenType.Reference,
                    AccessTokenType = AccessTokenType.Jwt,
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,
                    RequireConsent = false,

                    RedirectUris =           { "http://localhost:4200/home" },
                    PostLogoutRedirectUris = { "http://localhost:4200/unauthorized" },
                    AllowedCorsOrigins =     { "http://localhost:4200" },

                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Email,
                        "api1"
                       }
                }
            };

Have i missunderstood it?

eld1887 avatar Dec 14 '17 13:12 eld1887

This seems to be a general question about IdentityServer - not a bug report or an issue.

Please use one of the our free or commercial support options

See here for more details.

Thanks!

brockallen avatar Dec 14 '17 13:12 brockallen

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 22:01 lock[bot]