예제 #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddFreeSql(Configuration);

            services.AddCsRedisCore(Configuration);

            services.AddJwtBearer(Configuration);

            services.AddAutoMapper(typeof(UserProfile).Assembly, typeof(PoemProfile).Assembly);

            services.AddCors();

            #region Mvc

            services.AddMvc(options =>
            {
                options.ValueProviderFactories.Add(new ValueProviderFactory()); //设置SnakeCase形式的QueryString参数
                //options.Filters.Add<LogActionFilterAttribute>(); // 添加请求方法时的日志记录过滤器
                options.Filters.Add <LinCmsExceptionFilter>();                  //
            })
            .AddNewtonsoftJson(opt =>
            {
                opt.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:MM:ss";
                // 设置自定义时间戳格式
                opt.SerializerSettings.Converters = new List <JsonConverter>()
                {
                    new LinCmsTimeConverter()
                };
                // 设置下划线方式,首字母是小写
                opt.SerializerSettings.ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new SnakeCaseNamingStrategy()
                    {
                        //ProcessDictionaryKeys = true
                    },
                };
            })
            .ConfigureApiBehaviorOptions(options =>
            {
                //options.SuppressConsumesConstraintForFormFileParameters = true; //SuppressUseValidationProblemDetailsForInvalidModelStateResponses;
                //自定义 BadRequest 响应
                options.InvalidModelStateResponseFactory = context =>
                {
                    var problemDetails = new ValidationProblemDetails(context.ModelState);
                    var resultDto      = new UnifyResponseDto(ErrorCode.ParameterError, problemDetails.Errors, context.HttpContext);

                    return(new BadRequestObjectResult(resultDto)
                    {
                        ContentTypes = { "application/json" }
                    });
                };
            });
            services.AddSwaggerGenNewtonsoftSupport();
            #endregion

            //Swagger 扩展方法配置Swagger
            services.AddSwaggerGen();

            //配置Google验证码
            services.AddScoped <RecaptchaVerifyActionFilter>();
            services.Configure <GooglereCAPTCHAOptions>(Configuration.GetSection(GooglereCAPTCHAOptions.RecaptchaSettings));
            GooglereCAPTCHAOptions googlereCAPTCHAOptions = services.BuildServiceProvider().GetService <IOptionsSnapshot <GooglereCAPTCHAOptions> >().Value;

            if (googlereCAPTCHAOptions.Enabled)
            {
                services.AddreCAPTCHAV3(x =>
                {
                    x.VerifyBaseUrl = googlereCAPTCHAOptions.VerifyBaseUrl;
                    x.SiteKey       = googlereCAPTCHAOptions.SiteKey;
                    x.SiteSecret    = googlereCAPTCHAOptions.SiteSecret;
                });
            }

            services.AddDIServices(Configuration);

            //应用程序级别设置
            services.Configure <FormOptions>(options =>
            {
                //单个文件上传的大小限制为8 MB      默认134217728 应该是128MB
                options.MultipartBodyLengthLimit = 1024 * 1024 * 8; //8MB
            });

            // 分布式事务一致性CAP
            services.AddCap(Configuration);

            services.Configure <ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });

            //之前请注入AddCsRedisCore,内部实现IDistributedCache接口
            services.AddIpRateLimiting(Configuration);

            services.AddHealthChecks();
        }
예제 #2
0
 public RecaptchaVerifyActionFilter(ILogger <RecaptchaVerifyActionFilter> logger, IServiceProvider serviceProvider, IOptionsMonitor <GooglereCAPTCHAOptions> options)
 {
     _options    = options.CurrentValue;
     _siteVerify = _options.Enabled ? serviceProvider.GetRequiredService <IreCAPTCHASiteVerifyV3>() : null;
     _logger     = logger;
 }