Пример #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            ///mvc 加过滤器
            services.AddMvc(cfg =>
            {
                cfg.Filters.Add(typeof(HandleException));
            }).AddViewLocalization().AddDataAnnotationsLocalization();
            services.AddOptions();
            services.AddMemoryCache();
            services.AddSession(options =>
            {
                // Set a short timeout for easy testing.
                options.IdleTimeout = TimeSpan.FromMinutes(60);
            });
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath        = "/Login/Index";
                options.LogoutPath       = "/Login/Logout";
                options.AccessDeniedPath = "/Login/NoRight";
            });

            services.Configure <FormOptions>(options =>
            {
                options.MultipartBodyLengthLimit = 1024 * 1024 * 2;
            });
            services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.Configure <TestCore.MvcUtils.Admin.AppSettings>(Configuration.GetSection("AppSettings"));
            services.Configure <ApiSettings>(Configuration.GetSection("ApiSettings"));

            ///httpContext 使用
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            var ServiceProvider = IoCBootstrapper.Startup(services);

            return(ServiceProvider);
        }
Пример #2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //跨域
            services.AddCors(option =>
            {
                if (!string.IsNullOrEmpty(WebConfig.AppSettings.CorsOrigins))
                {
                    var urls = WebConfig.AppSettings.CorsOrigins.Split(',');
                    option.AddPolicy("AllowSameDomain", builder => builder.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin());
                }
                else
                {
                    option.AddPolicy("AllowSameDomain", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin());
                }
            });

            //*注入全局异常捕获*/
            services.AddMvc(o =>
            {
                o.Filters.Add(typeof(GlobalExceptions));
            });

            //HttpContext服务
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //配置文件json注册
            services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.Configure <JWTSettings>(Configuration.GetSection("Authentication").GetSection("JwtSettings"));
            services.Configure <AppSettings>(Configuration.GetSection("AppSettings"));
            services.Configure <ApiSettings>(Configuration.GetSection("ApiSettings"));
            services.Configure <RedisSettings>(Configuration.GetSection("RedisSetting"));

            //jwt服务
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(option =>
            {
                option.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = WebConfig.JWTSettings.Issuer,
                    ValidAudience    = WebConfig.JWTSettings.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(WebConfig.JWTSettings.Secret))
                };
            });
            //Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title   = "My API",
                    Version = "v1",
                });


                var filePath = Path.Combine(AppContext.BaseDirectory, "TestCore.API.xml");
                c.IncludeXmlComments(filePath);
                c.AddSecurityDefinition("Bearer", new ApiKeyScheme {
                    In = "header", Description = "请输入带有Bearer的Token,例如: \"Bearer {token}\"", Name = "Authorization", Type = "apiKey"
                });
                c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > {
                    {
                        "Bearer",
                        Enumerable.Empty <string>()
                    }
                });
            });

            //批量注册dll
            return(IoCBootstrapper.Startup(services));
        }