Example #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            return(services.UsePrimeServiceConfiguration <AppSettings>((options, appSettings) =>
            {
                options.ConfigureMvcBuilder = builder =>
                {
                    builder.ConfigureApiBehaviorOptions(mvcOptions =>
                    {
                        mvcOptions.InvalidModelStateResponseFactory = c =>
                        {
                            c.HttpContext.Response.StatusCode = 400;
                            Dictionary <string, IEnumerable <string> > modelErrors =
                                ValidationDictionary.GetErrorMessages(c.ModelState);

                            var result = new JsonResult(new { errors = modelErrors });
                            return result;
                        };
                    });
                };

                options.Swagger = _swaggerOptions;
                options.Swagger.ConfigureSwagger = swagger =>
                {
                    swagger.IgnoreObsoleteActions();

                    swagger.AddSecurityDefinition("jwt_auth", new OpenApiSecurityScheme()
                    {
                        Name = "Bearer",
                        BearerFormat = "JWT",
                        Scheme = "bearer",
                        Description = "Specify the authorization token.",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.Http,
                    });

                    swagger.AddSecurityRequirement(new OpenApiSecurityRequirement()
                    {
                        {
                            new OpenApiSecurityScheme()
                            {
                                Reference = new OpenApiReference()
                                {
                                    Id = "jwt_auth",
                                    Type = ReferenceType.SecurityScheme
                                }
                            },
                            Array.Empty <string>()
                        }
                    });
                };

                options.AdditionalServicesConfiguration = serv =>
                {
                    serv.AddJWTAuthentication(appSettings.JwtSettings);

                    serv.AddControllers()
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.Converters.Add(new StringEnumConverter());
                    })
                    .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                    });

                    serv.AddCors(opts => opts.AddPolicy(AnyOriginCors,
                                                        builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));

                    serv.AddHttpClient();

                    serv.AddHostedService <EpaadHostedService>();
                };

                options.EnableFluentValidation = true;
            }));
        }