Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //替换控制器所有者
            services.Replace(ServiceDescriptor.Transient <IControllerActivator, ServiceBasedControllerActivator>());

            services.AddAutoMapper();
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });
            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(AuthenticationFilter));
                options.Filters.Add(typeof(ValidationActionFilter));
            });

            //注入配置信息
            services.Configure <AppSettings>(Configuration.GetSection("AppSettings"));

            //添加jwt认证
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ClockSkew        = TimeSpan.FromSeconds(StaticConfiguration["jwt:expireseconds"].ToInt32(1800)),
                    ValidIssuer      = StaticConfiguration["jwt:issure"],
                    ValidAudience    = StaticConfiguration["jwt:audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(StaticConfiguration["jwt:securitykey"]))
                };
            });

            ApplicationContainer = AutofacBuilder.Builder(services);
            services.AddMvc();
            // Create the IServiceProvider based on the container.
            return(new AutofacServiceProvider(ApplicationContainer));
        }