2017-11-16 1 views
3

In der Startup.cs-Datei meines Identitätsservers habe ich den Server wie folgt konfiguriert. Ich benutze asp.net Identität für die Benutzerverwaltung.Wie erhalten Sie zusätzliche Ansprüche auf Client-Seite in Identity Server 4?

services.AddIdentityServer() 
       .AddDeveloperSigningCredential() 
       .AddInMemoryPersistedGrants() 
       .AddInMemoryIdentityResources(Config.GetIdentityResources()) 
       .AddInMemoryApiResources(Config.GetApiResources()) 
       .AddInMemoryClients(Config.GetClients()) 
       .AddAspNetIdentity<ApplicationUser>(); 

services.AddTransient<IProfileService, ProfileService>(); 

Meine IdentiyResources sind wie folgt. Hier möchte ich einen zusätzlichen Anspruch als IS_Token zurückgeben, den ich für eine weitere Geschäftslogik meiner Anwendung verwenden möchte.

public static IEnumerable<IdentityResource> GetIdentityResources() 
     { 
      return new List<IdentityResource> 
       { 
        new IdentityResources.OpenId(), 
        new IdentityResources.Profile(), 
        new IdentityResource("IS_token", new []{ "IS_token" }), 
       }; 
     } 

Ich füge auch diesen Anspruch in IssuedClaims in meinem IProfileService wie unten.

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

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

     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.UserName)); 
      claims.Add(new Claim(IdentityServerConstants.StandardScopes.Email, user.Email)); 

      //Get user claims from AspNetUserClaims table 
      var userClaims = await _userManager.GetClaimsAsync(user); 
      claims.AddRange(userClaims); 

      context.IssuedClaims = claims; 
     } 

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

Meine MVC-Client wie unten

   new Client 
        { 
         ClientId = "mvc", 
         ClientName = "MVC Client", 
         AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 
         RequireConsent = false, 

         ClientSecrets = 
         { 
          new Secret("secret".Sha256()) 
         }, 

         // where to redirect to after login 
         RedirectUris = { "http://localhost:5002/signin-oidc" }, 

         // where to redirect to after logout 
         PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" }, 

         AllowedScopes = new List<string> 
         { 
          IdentityServerConstants.StandardScopes.OpenId, 
          IdentityServerConstants.StandardScopes.Profile, 
          "IS_token", 
          "api" 
         }, 

         AllowOfflineAccess = true 
        }, 

In meiner MVC-Anwendung konfiguriert ist, habe ich den folgenden Code in Startup.cs Datei

services.AddMvc(); 

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 

    services.AddAuthentication(options => 
    { 
     options.DefaultScheme = "Cookies"; 
     options.DefaultChallengeScheme = "oidc"; 
    }) 
    .AddCookie("Cookies") 
    .AddOpenIdConnect("oidc", options => 
    { 
     options.SignInScheme = "Cookies"; 
     options.Authority = "http://localhost:5000"; 
     options.RequireHttpsMetadata = false; 

     options.ClientId = "mvc"; 
     options.ClientSecret = "secret"; 
     options.ResponseType = "code id_token"; 

     options.SaveTokens = true; 
     options.GetClaimsFromUserInfoEndpoint = true; 

     options.Scope.Add("api"); 
     options.Scope.Add("offline_access"); 
    }); 

Wenn ich Benutzer in Identitätsserver authentifizieren und erhalten zurück zu meinem gesicherten Link Ich habe folgende Ansprüche, aber IS_token fehlt in meinen Benutzeransprüchen.

Meine gesicherte Seite cshtml

<dl> 

    @foreach (var claim in User.Claims) 

    { 

     <dt>@claim.Type</dt> 

     <dd>@claim.Value</dd> 



    } 

    <dt>access token</dt> 
    <dd>@await ViewContext.HttpContext.GetTokenAsync("access_token")</dd> 

    <dt>refresh token</dt> 
    <dd>@await ViewContext.HttpContext.GetTokenAsync("refresh_token")</dd> 

</dl> 

Dies ist meine gesicherte Seite Screenshot

My secure page's output

Wie Sie im Bild sehen können IS_token fehlen. Wie bekomme ich IS_token Anspruch ???

Antwort

0

Ich muss AlwaysIncludeUserClaimsInIdToken = true in meiner Client-Konfiguration in Identity Server festlegen. Also sollte ich meinen Kunden wie folgt definieren.

   new Client 
        { 
         ClientId = "mvc", 
         ClientName = "MVC Client", 
         AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 
         RequireConsent = false, 

         ClientSecrets = 
         { 
          new Secret("secret".Sha256()) 
         }, 

         // where to redirect to after login 
         RedirectUris = { "http://localhost:5002/signin-oidc" }, 

         // where to redirect to after logout 
         PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" }, 

         AllowedScopes = new List<string> 
         { 
          IdentityServerConstants.StandardScopes.OpenId, 
          IdentityServerConstants.StandardScopes.Profile, 
          "IS_token", 
          "poslink" 
         }, 

         AlwaysIncludeUserClaimsInIdToken = true, 

         AllowOfflineAccess = true 
        }, 
+0

Dies wird nicht Ihnen helfen, denn das ID-Token ist nicht das Refresh-Token oder das Access-Token. –

0

In Ihrem Client-Konfiguration, in AddOpenIdConnect, sollten Sie hinzufügen:

options.Scope.Add("IS_token");

Ansonsten sind die Ansprüche dieses Umfangs werden durch die Implementierung von GetProfileDataAsync herausgefiltert

Verwandte Themen