private async Task <bool> CreateAccountAndAddToRole(CUserManager <Person> userManager, RoleManager <IdentityRole> roleManager, Person accountToCreate, string password, string roleName) { bool result = true; try { // Création du rôle si inexistant ! if (await roleManager.RoleExistsAsync(roleName) == false) { // Création du rôle var role = new IdentityRole { Name = roleName }; var identityRoleResult = await roleManager.CreateAsync(role); result &= identityRoleResult.Succeeded; } // Création du rôle var identityResult = await userManager.CreateAsync(accountToCreate, password); result &= identityResult.Succeeded; // Ajout du compte au rôle identityResult = await userManager.AddToRoleAsync(accountToCreate, roleName); result &= identityResult.Succeeded; } catch (Exception ex) { throw ex; } return(result); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, CUserManager <Person> userManager, RoleManager <IdentityRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { // Page d'erreur app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } // S'assurer que la base de données et les tables existent et si inexistant, les créer using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); if (context.Database.EnsureCreated()) { // On initilialise la base de données si elle est inexistante ! var initResult = Initialize(userManager, roleManager); initResult.Wait(); if (!initResult.Result) { throw new Exception("Database init failed !"); } } } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
private async Task <bool> Initialize(CUserManager <Person> userManager, RoleManager <IdentityRole> roleManager) { bool result = true; // Compte Administrateur Person admin = new Person { Email = Configuration["AdminInitEmail"], UserName = CUserManager <Person> .ADMINISTRATOR, Surname = string.Empty, Name = string.Empty, Address = string.Empty, Town = string.Empty, ZIPCode = string.Empty, PhoneNumber = string.Empty, Status = EntitiesEnums.EStatus.ACTIVE, Civility = Person.ECivility.SIR }; // Compte Client test Person client = new Person { Email = Configuration["TestAccountEmail"], Surname = "te", Name = "st", Address = string.Empty, Town = string.Empty, ZIPCode = string.Empty, PhoneNumber = string.Empty, Status = EntitiesEnums.EStatus.ACTIVE, Civility = Person.ECivility.SIR }; result &= await CreateAccountAndAddToRole(userManager, roleManager, admin, Configuration["AdminInitPassword"], GlobalResources.ROLE_ADMIN); result &= await CreateAccountAndAddToRole(userManager, roleManager, client, Configuration["TestAccountPassword"], GlobalResources.ROLE_CLIENT); return(result); }