示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var jwtTokenModel = new JwtConfigModel();

            Configuration.GetSection("JwtToken").Bind(jwtTokenModel);

            services.AddDbContext <AppIdentityDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity <AppIdentityUser, AppIdentityRole>()
            .AddEntityFrameworkStores <AppIdentityDbContext>()
            .AddDefaultTokenProviders();

            // ===== Add Jwt Authentication ========
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = jwtTokenModel.Issuer,
                    ValidAudience    = jwtTokenModel.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtTokenModel.Key)),
                    ClockSkew        = TimeSpan.Zero // remove delay of token when expire
                };
            });


            services.Configure <JwtConfigModel>(config => Configuration.GetSection("JwtToken").Bind(config));

            // Comment the next line if your app is running on the .NET Core 2.0
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            RepositoryInjection.Inject(services);

            services.AddMemoryCache(); // Adds a default in-memory
                                       // implementation of
                                       // IDistributedCache

            services.AddSession(options =>
            {
                options.IdleTimeout     = TimeSpan.FromMinutes(20);
                options.Cookie.HttpOnly = true;
            });

            services.AddAntiforgery(options =>
            {
                options.HeaderName  = "X-XSRF-TOKEN";
                options.Cookie.Name = "MyAntiForgeryCookieName";
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });
        }