Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        //public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        //{
        //    if (env.IsDevelopment())
        //    {
        //        app.UseDeveloperExceptionPage();
        //    }

        //    app.UseMvc();
        //}
        private void ConfigurePolicies(IServiceCollection services)
        {
            var appSettings = new Framework.Builders.AppSettings();
            var repository  = new AccessControlRepository(appSettings, new Common.Factories.AshleyHsDbConnectionFactory(appSettings));
            AccessControlService accessControlService     = new AccessControlService(repository);
            List <Common.Models.SecureResource> resources = accessControlService.GetResourceListAsync().Result as List <Common.Models.SecureResource>;

            foreach (var resource in resources)
            {
                foreach (var accessControl in resource.AccessControl) //not efficient but the numbers should be small
                {
                    var roles = accessControl.Value.Select(role => role.Name).ToList();

                    services.AddAuthorization(options =>
                                              options.AddPolicy($"{resource.Name.ToLower()}_{accessControl.Key.ToLower()}_roles",
                                                                policy =>
                    {
                        policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
                        policy.Requirements.Add(new RoleListRequirement(roles));
                    }
                                                                ));
                }
            }
        }