// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); // Add application services. services.AddTransient <IEmailSender, EmailSender>(); services.AddScoped <IPersonnelService, PersonnelService>(); services.AddScoped <IDocumentService, DocumentService>(); services.AddScoped <IFileDocumentService, FileDocumentService>(); services.AddScoped <IPermitService, PermitService>(); services.AddAuthorization(options => { foreach (var role in ApplicationRoles.GetAllRoles()) { options.AddPolicy("Require" + role, p => { p.RequireRole(role); }); } }); services.AddMvc(); }
private async Task CreateRoles(IServiceProvider serviceProvider) { RoleManager <IdentityRole> _roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); foreach (string role in ApplicationRoles.GetAllRoles()) { var roleExist = await _roleManager.RoleExistsAsync(role); if (!roleExist) { await _roleManager.CreateAsync(new IdentityRole(role)); } } }