2017-12-14 4 views
0

userinfo 끝점을 통해 사용자 지정 클레임을 가져오고 싶습니다. 그것도 작동하지만 모든 사용자 정의 클레임도 액세스 토큰에 있습니다. 그런 식으로, 액세스 토큰에 하나 또는 두 개의 클레임 (예 : 전자 메일 또는 이름)을 넣을 수 있고 다른 모든 클레임 (given_name, ...)은 userinfo 끝점을 통해 액세스 할 수있는 가능성이 있다는 것을 이해합니다.userinfo 끝점을 통해서만 소유권 주장을 얻는 방법 및 액세스 토큰이 아닌 방법

내 ProfileService 그렇게보고있다 (그리고 그들은 액세스 토큰에없는) :

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; 
     } 
    } 

이 내 config.cs 파일입니다

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" 
         } 
       } 
      }; 

내가 그것을 missunderstand나요?

답변

0

GetProfileDataAsync은 두 번 호출되지만 다른 컨텍스트를 사용합니다. Context.Caller = ClaimsProviderAccessToken 액세스 토큰에 대한

  1. .
  2. 신원 토큰의 경우 Context.Caller = UserInfoEndpoint.

두 상황 모두 요청한 소유권 주장이 다릅니다.

ID에 대한 소유권 주장 만 추가하려는 경우 필터 (IdentityResource)에 추가하여 이러한 소유권 주장을 포함하도록 identityserver를 구성 할 수 있습니다.이 경우에는 GetProfileDataAsync에 추가 소유권 주장을 전혀 추가하지 않아도됩니다. . 또는 특정 소유권 주장을 추가하려는 경우 현재 컨텍스트를 확인하십시오. 이것은 사용자 정보에만 청구를 추가해야

if (Context.Caller == "UserInfoEndpoint") 
    claims.Add(new Claim(JwtClaimTypes.GivenName, user.FirstName)); 

:

그래서 GetProfileDataAsync에서 다음과 같은 것을 가질 수 있습니다.

+0

설명해 주셔서 감사합니다. 그게 정확히 내가 찾던 것이 었어. – eldios1981

관련 문제