Exemple #1
0
        public IServiceCollection ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IConfiguration>(provider => Configuration);

            ConfigureQueries.ConfigureDependenciesQueries(services);
            ConfigureRepository.ConfigureDependenciesRepository(services, Configuration);
            ConfigureService.ConfigureDependenciesService(services);

            //Add security context
            var signingConfiguration = new SigningConfiguration();

            services.AddSingleton(signingConfiguration);

            var tokenConfiguration = new TokenConfiguration();

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

            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());
            });

            return(services);
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureQueries.ConfigureDependenciesQueries(services);
            ConfigureRepository.ConfigureDependenciesRepository(services, Configuration);
            ConfigureService.ConfigureDependenciesService(services);

            //Add mapper
            var configMapper = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new DtoToEntityProfile());
                cfg.AddProfile(new EntityToModelProfile());
            });
            IMapper mapper = configMapper.CreateMapper();

            services.AddSingleton(mapper);

            //Add security context
            var signingConfiguration = new SigningConfiguration();

            services.AddSingleton(signingConfiguration);

            var tokenConfiguration = new TokenConfiguration();

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

            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());
            });

            //Add swager context
            services.AddSwaggerGen(s =>
            {
                s.SwaggerDoc("v1",
                             new OpenApiInfo
                {
                    Title       = "Zurich Teste",
                    Description = "C[alculo de Seguro",
                    Contact     = new OpenApiContact
                    {
                        Name  = "Ivan Souza",
                        Email = "*****@*****.**",
                        Url   = new Uri("http://www.ivanssouza.com.br")
                    }
                });
                s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    In          = ParameterLocation.Header,
                    Description = "Enter with Jwt Token",
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
                s.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header
                        },
                        new List <string>()
                    }
                });
            });

            services.AddControllers();
        }