示例#1
0
        public void ConfigureServices(IServiceCollection services)
        {
            Globalization(services);

            services
            .AddMvcCore()
            .AddVersionedApiExplorer(options =>
            {
                options.GroupNameFormat           = "'v'VVV";
                options.SubstituteApiVersionInUrl = true;
            });


            services.AddMvc()
            .AddDataAnnotationsLocalization()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

            services.AddApiVersioning();

            services.AddSwaggerGen(
                options =>
            {
                var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>();

                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
                }
            });

            var jwtConfiguration = Configuration
                                   .GetSection(nameof(JwtTokenSettings));

            services.Configure <JwtTokenSettings>(jwtConfiguration);
            var jwtTokenSettings = jwtConfiguration
                                   .Get <JwtTokenSettings>();

            services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(o =>
            {
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateLifetime         = true,
                    ValidateAudience         = false,
                    ValidateIssuer           = false,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(UTF8
                                                                        .GetBytes(jwtTokenSettings.IssuerSigningKey))
                };
            });

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

            services.AddAuthorization(options =>
            {
                options.AddPolicy("ElevatedRights", policy => policy.RequireClaim("a"));
            });

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

            BootStrap.RegistrarServico(services);

            services.Configure <IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });

            services.AddDbContext <ContextoDominio>(options => options.UseSqlServer(
                                                        Configuration.GetConnectionString("ConexaoDev"),
                                                        sqlOptions => sqlOptions.EnableRetryOnFailure(
                                                            maxRetryCount: 3,
                                                            maxRetryDelay: TimeSpan.FromHours(3),
                                                            errorNumbersToAdd: null)));

            //services.AddDbContext<ContextoCliente>(options => options.UseSqlServer(
            //    Configuration.GetConnectionString("ConexaoDev"),
            //    sqlOptions => sqlOptions.EnableRetryOnFailure(
            //        maxRetryCount: 3,
            //        maxRetryDelay: TimeSpan.FromHours(3),
            //        errorNumbersToAdd: null)));
        }