Exemple #1
0
        internal static IServiceCollection AddWebApiConfiguration(this IServiceCollection services, IConfiguration configuration, out Configurations.App.WebApiConfiguration webApiConfiguration)
        {
            Log.Information("{method}", nameof(AddWebApiConfiguration));

            webApiConfiguration = configuration.GetSection(nameof(Configurations.App.WebApiConfiguration)).Get <Configurations.App.WebApiConfiguration>();

            services.AddSingleton(webApiConfiguration);

            return(services);
        }
Exemple #2
0
        internal static IServiceCollection ConfigureSwaggerGenerator(this IServiceCollection services, Configurations.App.WebApiConfiguration webApiConfiguration, Configurations.Security.AuthorityConfiguration authorityConfiguration, Configurations.Swagger.SwaggerUIConfiguration swaggerConfiguration)
        {
            Log.Information("{method}", nameof(ConfigureSwaggerGenerator));

            services.AddSwaggerGen(setupAction =>
            {
                setupAction.SwaggerDoc(webApiConfiguration.WebApiVersion, new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Contact = new Microsoft.OpenApi.Models.OpenApiContact
                    {
                        Name  = webApiConfiguration.WebApiContactName,
                        Email = webApiConfiguration.WebApiContactMail,
                        Url   = new Uri(webApiConfiguration.WebApiContactUrl)
                    },
                    Title       = webApiConfiguration.WebApiTitle,
                    Version     = webApiConfiguration.WebApiVersion,
                    Description = webApiConfiguration.WebApiDescription
                });

                setupAction.IncludeXmlComments(System.IO.Path.Combine(AppContext.BaseDirectory,
                                                                      $"{webApiConfiguration.GetType().Assembly.GetName().Name}.xml"));

                setupAction.AddSecurityDefinition("OAuth2", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
                {
                    Type  = Microsoft.OpenApi.Models.SecuritySchemeType.OAuth2,
                    Flows = new Microsoft.OpenApi.Models.OpenApiOAuthFlows
                    {
                        AuthorizationCode = new Microsoft.OpenApi.Models.OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri($"{authorityConfiguration.Authority}/connect/authorize"),
                            TokenUrl         = new Uri($"{authorityConfiguration.Authority}/connect/token"),
                            Scopes           = new Dictionary <string, string> {
                                { swaggerConfiguration.SwaggerUITargetApiScope, swaggerConfiguration.SwaggerUITargetApiScopeTitle }
                            }
                        }
                    },
                    Description = "Swagger Generator OpenId security scheme."
                });

                setupAction.OperationFilter <Configurations.Authorization.AuthorizeCheckOperationFilter>();
            });

            return(services);
        }