// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context, RoleManager <ApplicationRole> roleManager, UserManager <ApplicationUser> userManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); InitialUser.Initialize(app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope().ServiceProvider); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); // DummyData.Initialize(context, userManager, roleManager).Wait(); }
public async Task <IActionResult> Index(InitialUser model) { if ((await _userManager.GetUsersInRoleAsync(ApplicationRoleManager.Administrator)).Count > 0) { return(View("SetupComplete")); } await CreateInitialUser(model); return(View("SetupComplete")); }
private async Task CreateInitialUser(InitialUser model) { var initialUser = new ApplicationUser { UserName = model.UserName, IsApproved = true }; var userResult = await _userManager.CreateAsync( initialUser, model.Password ); if (userResult.Succeeded) { await _userManager.AddToRoleAsync(initialUser, ApplicationRoleManager.Administrator); await _userManager.AddToRoleAsync(initialUser, ApplicationRoleManager.MultiSyndicate); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddSerilog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } CultureInfo.CurrentCulture = new CultureInfo("en-US"); app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US") }); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); InitialUser.Initialize(app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope().ServiceProvider); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
async Task EnsureUserFromConfiguration(UserManager <ApplicationUser> userManager, InitialUser user) { var existingUser = await userManager.FindByEmailAsync(user.EmailAddress); if (existingUser != null && !string.IsNullOrWhiteSpace(user.Password)) { //var passwordResetToken = await userManager.GeneratePasswordResetTokenAsync(existingUser); //await userManager.ResetPasswordAsync(existingUser, passwordResetToken, user.Password); //Console.WriteLine($"User {existingUser.Email} has been given a new password from configuration"); return; } var applicationUser = new ApplicationUser { Id = user.Id.ToLower(), Email = user.EmailAddress, UserName = user.EmailAddress, NormalizedEmail = user.EmailAddress.Normalize(), NormalizedUserName = user.EmailAddress.Normalize() }; await userManager.CreateAsync(applicationUser, user.Password); Console.WriteLine($"User {user.EmailAddress} has been added from configuration"); }