Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            ServiceContainer.ConfigureServiceDependencies(services);
            RepositoryContainer.ConfigureRepositoryDependencies(services);

            var autoMapperConfig = new MapperConfiguration(config =>
            {
                config.AddProfile(new DTOToModelProfile());
                config.AddProfile(new EntityToDTOProfile());
                config.AddProfile(new ModelToEntityProfile());
            });

            IMapper mapper = autoMapperConfig.CreateMapper();

            services.AddSingleton(mapper);

            var signingConfiguration = new SigningConfigurations();

            services.AddSingleton(signingConfiguration);

            var tokenConfiguration = new TokenConfiguration();

            new ConfigureFromConfigurationOptions <TokenConfiguration>(
                Configuration.GetSection("TokenConfiguration"))
            .Configure(tokenConfiguration);
            services.AddSingleton(tokenConfiguration);

            services.AddControllers();

            services.AddAuthentication(authOptions =>
            {
                authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                authOptions.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(bearerOptions =>
            {
                var paramsValidation = bearerOptions.TokenValidationParameters;
                paramsValidation.IssuerSigningKey         = signingConfiguration.Key;
                paramsValidation.ValidAudience            = tokenConfiguration.Audience;
                paramsValidation.ValidIssuer              = tokenConfiguration.Issuer;
                paramsValidation.ValidateIssuerSigningKey = true;
                paramsValidation.ValidateLifetime         = true;
                paramsValidation.ClockSkew = TimeSpan.Zero;
            });

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                               .RequireAuthenticatedUser()
                               .Build()
                               );
            });

            services.AddSwaggerGen(swagger =>
            {
                swagger.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Api .Net Core DDD",
                    Description = "Domain driven design implemented on a .Net Core webapi",
                    Contact     = new OpenApiContact
                    {
                        Email = "*****@*****.**",
                        Name  = "Vinicius Vassão",
                        Url   = new Uri("https://github.com/vassourita")
                    }
                });

                swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "Use a JWT Token",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });

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