Exemple #1
0
        public TestIntegrationBase()
        {
            OpenConnection();
            Service = new ServiceCollection();

            var tokenConfiguration = new TokenConfiguration
            {
                Audience = "test",
                Hours    = 1,
                Issuer   = "test"
            };

            Service.AddSingleton <ITokenConfiguration>(tokenConfiguration);

            var signingConfiguration = new SigningConfiguration();

            signingConfiguration.GenerateKey();
            Service.AddSingleton <ISigningConfiguration>(signingConfiguration);

            Service.AddAutoMapper(typeof(TestIntegrationBase));
            Service.Register();
            Service.AddScoped(provider => DbContext);
            Service.AddScoped <IMediator, MediatorFake>();
            Service.AddScoped <INotificationHandler <DomainNotification>, DomainNotificationHandler>(provider => DomainNotificationHandler);
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <BookContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("Default"));
            });

            var tokenConfiguration = new TokenConfiguration();

            Configuration.GetSection("TokenConfigurations").Bind(tokenConfiguration);
            services.AddSingleton <ITokenConfiguration>(tokenConfiguration);

            var signingConfiguration = new SigningConfiguration();

            signingConfiguration.GenerateKey();
            services.AddSingleton <ISigningConfiguration>(signingConfiguration);

            services.AddCors(options =>
            {
                options.AddPolicy("policy", builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyMethod();
                    builder.AllowAnyHeader();
                    builder.SetIsOriginAllowed(_ => true);
                    builder.WithExposedHeaders("Content-Disposition");
                });
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = signingConfiguration.Key,
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidIssuer      = tokenConfiguration.Issuer,
                    ValidAudience    = tokenConfiguration.Audience,
                    ValidateLifetime = true,
                    ClockSkew        = TimeSpan.Zero
                };
            });

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



            services.AddControllers(options =>
            {
                options.Filters.Add(typeof(GlobalExceptionHandlingFilter));
                options.Filters.Add(new AuthorizeFilter("Bearer"));
            }).ConfigureApiBehaviorOptions(options => options.SuppressModelStateInvalidFilter = true)
            .AddNewtonsoftJson(x => { x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });


            services.AddAutoMapper(typeof(AutoMapperConfig));
            AutoMapperConfig.RegisterMappings();
            services.Register();
            services.AddMediatR(typeof(Startup));
        }