/// <summary> /// Add open Api with builder /// </summary> /// <param name="services"></param> /// <param name="optBuilder"></param> public static void AddOpenApi(this IServiceCollection services, Action <OpenApiOptions> optBuilder) { var opts = new OpenApiOptions(); optBuilder?.Invoke(opts); services.Configure(optBuilder); services.AddSwaggerGen(options => { options.SchemaFilter <SchemaExtensionFilter>(); options.DocumentFilter <SecurityDocumentFilter>(); options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("swagger", new Info { Title = opts.Id?.ToUpper(), Version = opts.Version, Description = opts.Description }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{opts.Authority}/connect/authorize", TokenUrl = $"{opts.Authority}/connect/token", Scopes = new Dictionary <string, string> { { opts.Id, opts.Id?.ToUpper() } } }); }); }
/// <summary> /// Add open Api with builder /// </summary> /// <param name="services"></param> /// <param name="optBuilder"></param> public static void AddOpenApi(this IServiceCollection services, Action <OpenApiOptions> optBuilder) { services.AddOptions(); optBuilder ??= options => {}; services.Configure(optBuilder); var opts = new OpenApiOptions(); optBuilder.Invoke(opts); services.AddSwaggerGen(c => { c.SchemaFilter <SchemaExtensionFilter>(); c.SwaggerDoc("swagger", new OpenApiInfo { Title = opts.Id?.ToUpper(), Version = opts.Version, Description = opts.Description }); if (string.IsNullOrEmpty(opts.Authority)) { return; } var scopes = opts.ExtraScopes?.ToDictionary(s => s, s => s.ToUpper()) ?? new Dictionary <string, string>(); if (opts.Id != null) { scopes.Add(opts.Id, opts.Description); } c.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows { Implicit = new OpenApiOAuthFlow { AuthorizationUrl = new Uri($"{opts.Authority}/connect/authorize"), TokenUrl = new Uri($"{opts.Authority}/connect/token"), Scopes = scopes } }, }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "OAuth2" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, scopes.Keys.ToList() } }); }); }