// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Contacts API", Version = "v1" }); }); services.AddMvc(); services.AddMemoryCache(); services.AddResponseCaching(); services.Configure <DatabaseOptions>(Configuration.GetSection("sql")); services.Configure <JwtOptions>(Configuration.GetSection("jwt")); services.AddEntityFrameworkSqlServer() .AddEntityFrameworkInMemoryDatabase() .AddDbContext <AuthenticationServerDbContext>(); services.AddAuthorization(a => a.AddPolicy("admin", policy => policy.RequireRole("admin"))); services.AddAuthorization(b => b.AddPolicy("user", policyb => policyb.RequireRole("user"))); var jwtOptions = new JwtOptions(); Configuration.GetSection("jwt").Bind(jwtOptions); // ===== 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.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = jwtOptions.Issuer, ValidateAudience = false, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecretKey)) }; }); // Configute Autofac var builder = new ContainerBuilder(); // Loads the already configured items from services object builder.Populate(services); builder.RegisterAssemblyTypes(typeof(Startup).Assembly) .AsImplementedInterfaces() .InstancePerLifetimeScope(); RepositoryContainer.Update(builder); Container = builder.Build(); return(new AutofacServiceProvider(Container)); }