Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDbContext <LeaseManagementDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LeaseManagementDbContext")));
            services.AddAuthorization();

            // IOC CONFIGURATION

            Business.IoCConfig.ConfigContainer(services);
            IoCConfig.ConfigContainer(services);


            // JWT CONFIGURATION

            var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"]));
            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,
                ValidateIssuer           = false,
                ValidateAudience         = false,
                ValidateLifetime         = true,
                ClockSkew             = TimeSpan.Zero,
                RequireExpirationTime = true,
            };

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "JWTAuth";
            })
            .AddJwtBearer("JWTAuth", x =>
            {
                x.RequireHttpsMetadata      = false;
                x.TokenValidationParameters = tokenValidationParameters;
            });

            // SWAGGER CONFIGURATION

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "LeaseManagement", Version = "v1"
                });
            });

            // CORS CONFIGURATION

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder => builder.AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            });
        }