2017-09-29 14 views
1

J'ai configuré IdentityServer3 localement et tout fonctionne correctement. J'utilise JWT pour autoriser mes utilisateurs et avoir réussi à accéder à mes contrôleurs d'API Web (avec l'attribut authorize).configuration IdentityServer3 dans azure

Lorsque j'ai téléchargé sur azure, bien que je puisse obtenir un jeton d'accès, mais quand j'essaye d'accéder à un contrôleur, j'obtiens une erreur 401. Je suppose que cela a à voir avec le certificat. Ma configuration ressemble à ceci:

public static class Config 
{ 

    /// <summary> 
    /// Configures identity server 
    /// </summary> 
    public static void ConfigureIdentityServer(this IAppBuilder app, CormarConfig config) 
    { 

     // Create our options 
     var identityServerOptions = new IdentityServerOptions 
     { 
      SiteName = "Cormar API", 
      SigningCertificate = LoadCertificate(), 
      IssuerUri = "https://localhost:44313", 

      // Not needed 
      LoggingOptions = new LoggingOptions 
      { 
       EnableHttpLogging = true, 
       EnableWebApiDiagnostics = true, 
       EnableKatanaLogging = true, 
       WebApiDiagnosticsIsVerbose = true 
      }, 

      // In membory crap just to get going 
      Factory = new IdentityServerServiceFactory().Configure(config),   

      // Disable when live 
      EnableWelcomePage = true 
     }; 

     // Setup our auth path 
     app.Map("/identity", idsrvApp => 
     { 
      idsrvApp.UseIdentityServer(identityServerOptions); 
     }); 
    } 


    /// <summary> 
    /// Configures the identity server to use token authentication 
    /// </summary> 
    public static void ConfigureIdentityServerTokenAuthentication(this IAppBuilder app, HttpConfiguration config) 
    { 
     app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "https://localhost:44313/identity", 
      ValidationMode = ValidationMode.ValidationEndpoint, 
      RequiredScopes = new[] { "api" } 
     }); 

     AntiForgeryConfig.UniqueClaimTypeIdentifier = IdentityServer3.Core.Constants.ClaimTypes.Subject; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 
    } 

    /// <summary> 
    /// Loads the certificate 
    /// </summary> 
    /// <returns></returns> 
    private static X509Certificate2 LoadCertificate() 
    { 
     var certPath = $"{ AppDomain.CurrentDomain.BaseDirectory }App_Data\\idsrv3test.pfx"; 
     return new X509Certificate2(certPath, "idsrv3test"); 
    } 

    /// <summary> 
    /// Configure the identity service factory with custom services 
    /// </summary> 
    /// <returns></returns> 
    private static IdentityServerServiceFactory Configure(this IdentityServerServiceFactory factory, CormarConfig config) 
    { 
     var serviceOptions = new EntityFrameworkServiceOptions { ConnectionString = config.SqlConnectionString }; 
     factory.RegisterOperationalServices(serviceOptions); 
     factory.RegisterConfigurationServices(serviceOptions); 

     factory.CorsPolicyService = new Registration<ICorsPolicyService>(new DefaultCorsPolicyService { AllowAll = true }); // Allow all domains to access authentication 
     factory.Register(new Registration<DbContext>(dr => dr.ResolveFromAutofacOwinLifetimeScope<DbContext>())); 
     factory.UserService = new Registration<IUserService>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IUserService>()); 
     factory.ClientStore = new Registration<IClientStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IClientStore>()); 
     factory.ScopeStore = new Registration<IScopeStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IScopeStore>()); 

     return factory; 
    } 
} 

J'ai lu et il semble que si j'utilise des jetons de référence, je ne ai pas besoin d'utiliser un certificat pour les signer. Alors j'ai changé mon client AccessTokenType pour faire référence à jeton et ajouté le secret à la api portée et j'ai pu accéder à mes contrôleurs protégés localement, mais encore une fois quand je pousse à azur, je reçois encore 401.

Est-ce que quelqu'un sait comment je peux résoudre ce problème?

+0

mais si fonctionne en azur, pourquoi avez-vous localhost et le port 44313? et ce port est-il ouvert dans le pare-feu Azure? (Configuration du site)? – rmjoia

+0

Vous pouvez toujours tweeter [Dominick Baier] (https://twitter.com/leastprivilege) – rmjoia

+0

@rmjoia l'émetteur peut être modifié à tout moment. C'est juste le localhost lors des tests, mais cela ne provoque pas le problème que j'ai – r3plica

Répondre

1

Modifier les paramètres pour UseIdentityServerBearerTokenAuthentication était la solution à ce problème. Je mis à jour les options pour cela:

DelayLoadMetadata = true, 

Et tout a commencé à travailler.