// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddScoped <IUsersRepository, UsersRepository>(); services.AddScoped <ITeachersAccountRepository, TeachersAccountRepository>(); services.AddScoped <IUsersServices, UsersService>(); services.AddScoped <ITeachersAccountService, TeacherAccountService>(); services.AddDbContext <AppDbContext>(); AppDbContext dbContext = new AppDbContext(); IAuthentication authentication = new AuthenticationHandler(dbContext); services.AddAuthorization(options => { options.AddPolicy("Admin", policy => policy.RequireAssertion(async context => { String receive = ((AuthorizationFilterContext)(context.Resource)).HttpContext.Request.Headers["Authorization"]; if (receive != null) { string[] words = receive.Split(' '); TeacherAccount result = await authentication.FindTeacherAccount(words[0], words[1]); return(result != null); } else { return(false); } })); }); services.AddAuthorization(options => { options.AddPolicy("User", async policy => policy.RequireAssertion(async context => { String receive = ((AuthorizationFilterContext)(context.Resource)).HttpContext.Request.Headers["Authorization"]; if (receive != null) { string[] words = receive.Split(' '); TeacherAccount result = await authentication.FindTeacherAccount(words[0], words[1]); User resultUser = await authentication.FindUserAccount(words[0], words[1]); if (result != null || resultUser != null) { return(true); } else { return(false); } } else { return(false); } })); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }