Skip to content

Commit 80ce069

Browse files
committed
check to see if identity authority is setup before adding security to swagger
1 parent f9184c8 commit 80ce069

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/Cortside.AspNetCore.Swagger/ServiceCollectionExtensions.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,22 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IC
9595
//c.TagActionsBy(apiDescription => new[] { apiDescription.RelativePath });
9696

9797
var identityServerConfiguration = configuration.GetSection("IdentityServer").Get<IdentityServerConfiguration>();
98-
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme {
99-
Type = SecuritySchemeType.OAuth2,
100-
Flows = new OpenApiOAuthFlows {
101-
ClientCredentials = new OpenApiOAuthFlow {
102-
AuthorizationUrl = new Uri($"{identityServerConfiguration.Authority}/connect/authorize"),
103-
TokenUrl = new Uri($"{identityServerConfiguration.Authority}/connect/token"),
104-
Scopes = new Dictionary<string, string> {
105-
{identityServerConfiguration.ApiName, identityServerConfiguration.ApiName}
98+
var authority = identityServerConfiguration?.Authority;
99+
100+
if (!string.IsNullOrWhiteSpace(authority)) {
101+
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme {
102+
Type = SecuritySchemeType.OAuth2,
103+
Flows = new OpenApiOAuthFlows {
104+
ClientCredentials = new OpenApiOAuthFlow {
105+
AuthorizationUrl = new Uri($"{authority}/connect/authorize"),
106+
TokenUrl = new Uri($"{authority}/connect/token"),
107+
Scopes = new Dictionary<string, string> {
108+
{ identityServerConfiguration.ApiName, identityServerConfiguration.ApiName }
109+
}
106110
}
107111
}
108-
}
109-
});
112+
});
113+
}
110114
});
111115

112116
services.AddSwaggerGenNewtonsoftSupport();

0 commit comments

Comments
 (0)