Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureDI(services);

            services.AddCors(); // disable to enforce strict security on app


            // connect to SQL Server database
            string conn = Configuration.GetConnectionString("DDD.Conn");

            services.AddDbContext <AppDbContext>(opt =>
            {
                opt.UseSqlServer(conn);
                opt.EnableSensitiveDataLogging(); // turn off as required to keep logs cleaner
            });

            services.AddLogging();

            // register HealthCheck services
            services.AddHealthChecks()
            .AddDbContextCheck <AppDbContext>("SQL Server Database")
            .AddCheck <ThirdPartyServiceHealthCheck>("ThirdParty Services")
            .AddCheck <PaymentGatewayHealthCheck>("Payment Gateway");


            services.AddAuthentication("Bearer")
            .AddJwtBearer("Bearer", options =>
            {
                options.Authority            = "APIStore";
                options.RequireHttpsMetadata = false;
                options.Audience             = "APIStore";
            });

            // load clients into app memory
            // on add of new client, service must be restarted

            var clients = new List <AppClient>();

            Configuration.GetSection("AppClients").Bind(clients);
            services.AddSingleton(clients);

            services.AddAuthorization(opt =>
            {
                opt.DefaultPolicy = new AuthorizationPolicyBuilder()
                                    .AddRequirements(new APIKeyStore(AppClient.LoadClients(clients)))
                                    .Build();
            });

            // setup Swagger
            ConfigureSwagger(services);
        }