2017-12-14 13 views
0

私はuserinfoエンドポイント経由でカスタムクレームを取得したいと考えています。これも機能しますが、すべてのカスタムクレームもアクセストークンに含まれています。私はアクセストークンに1つまたは2つのクレーム(電子メールまたは名前など)を入れ、他のすべてのクレーム(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

は、異なるコンテキストで2回呼び出されます。 Context.Caller = ClaimsProviderAccessTokenアクセストークンのために

  1. IDトークンの場合Context.Caller = UserInfoEndpoint

両方の状況について、要求される主張は異なります。

IDのみの申し立てを追加する場合は、これらのクレームをフィルタに追加して(IdentityResource)identityserverを設定することができます。その場合は、GetProfileDataAsyncにクレームを追加する必要はありません。特定のクレームを追加する場合は、現在のコンテキストを確認してください。

のでGetProfileDataAsyncには、次のようなものかもしれない:これはのみのuserinfoに請求を追加する必要があります

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

を。

+0

ありがとうございます。それはまさに私が探していたものでした – eldios1981

関連する問題