2017-03-05 1 views
3

Wir haben die Swagger für Web-API-Anwendung aktiviert, die auf Azure Service Fabric gehostet wird. Wir möchten die Sicherheit auf Swagger UI aktivieren. Also folgte ich unten URL, die Sicherheit ermöglicht - https://blogs.msdn.microsoft.com/pratushb/2016/04/28/enable-swagger-to-authenticate-against-azure-ad/ https://github.com/domaindrivendev/Swashbuckle/issues/671 (Antwort von Oleksandr-Tokmakov)Aktivieren von Azure AD Auth für Swagger UI, gehostet in Service Fabric

ich das „Verfügbare Berechtigungen“ Pop-up sehen konnte, und ich konnte AAD-Authentifizierung durchgeführt erfolgreich auf einem anderen Reiter auf Klick auf Genehmigen Knopf. Aber sobald die Authentifizierung abgeschlossen ist, sehe ich, dass das Token nicht an swagger ui zurückgegeben wird und die Authentifizierungsregisterkarte nicht geschlossen wird.

Unten ist der Code, den ich verwendet habe. (Ich habe zwei AAD, eine für Web Services gehostet auf Service-Fabric und eine andere für Swagger UI)

config.EnableSwagger(
    c => 
    { 
     c.SingleApiVersion("v1", "Title of Service"); 
     c.OAuth2("oauth2") 
      .Description("OAuth2 Implicit Grant") 
      .Flow("implicit") 
      .AuthorizationUrl("https://login.microsoftonline.com/tenentId-guid/oauth2/authorize") 
      .Scopes(scopes => 
      { 
       scopes.Add("user_impersonation", "Access Services Local Swagger Secure"); 
      }); 
     c.OperationFilter<AssignOAuth2SecurityRequirements>(); 
    } 
).EnableSwaggerUi(c => 
{ 
    c.EnableOAuth2Support(
      clientId: "Swagger AAD application Client Id", 
      clientSecret: "Swagger AAD application Key", 
      realm: "https://localhost:444/swagger/ui/o2c-html", 
      appName: "https://serviceslocal/swagger/", // Free text, no reference to AAD 
      scopeSeperator: "", 
      additionalQueryStringParams: new Dictionary<string, string>() { { "resource", "Web API AAD application Client Id" } } 
     ); 
} 
); 


public class AssignOAuth2SecurityRequirements : IOperationFilter 
    { 
     public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
     { 
      // Correspond each "Authorize" role to an oauth2 scope 
      var scopes = apiDescription.ActionDescriptor.GetFilterPipeline() 
       .Select(filterInfo => filterInfo.Instance) 
       .OfType<AuthorizeAttribute>() 
       .SelectMany(attr => attr.Roles.Split(',')) 
       .Distinct(); 
      if (scopes.Any()) 
      { 
       if (operation.security == null) 
        operation.security = new List<IDictionary<string, IEnumerable<string>>>(); 
       var oAuthRequirements = new Dictionary<string, IEnumerable<string>> 
      { 
       { "oauth2", scopes } 
      }; 
       operation.security.Add(oAuthRequirements); 
      } 
     } 
    } 

Antwort

0

Ich verwende Auth0, aber hier ist, was ich für OAuth2 bekam, die für mich funktioniert.

options.AddSecurityDefinition("oauth2", new OAuth2Scheme 
{ 
    Type = "oauth2", 
    Flow = "implicit", 
    AuthorizationUrl = Path.Combine(auth0Settings["Authority"].Value, "authorize") 
}); 

app.UseSwaggerUI(c => 
{ 
    c.RoutePrefix = "docs"; 
    c.InjectOnCompleteJavaScript("/swagger-ui.js"); 
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Easy Streak API"); 
    c.ConfigureOAuth2(auth0Settings["ClientId"].Value, auth0Settings["ClientSecret"].Value, auth0Settings["Authority"].Value, "EasyStreak API"); 
}); 
Verwandte Themen