private async void CreateRootUser(IServiceProvider serviceProvider, RootUserConfig rootUserConfig) { UserManager <User> userManager = serviceProvider.GetRequiredService <UserManager <User> >(); RoleManager <IdentityRole> roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); IdentityRole adminRole = await roleManager.FindByNameAsync("Administrator"); if (adminRole == null) { adminRole = new IdentityRole(); adminRole.Name = "Administrator"; adminRole.NormalizedName = "ADMINISTRATOR"; await roleManager.CreateAsync(adminRole); } User user = new User { UserName = rootUserConfig.Username, Email = rootUserConfig.Email }; IdentityResult result = await userManager.CreateAsync(user, rootUserConfig.Password); if (result.Succeeded) { await userManager.AddToRoleAsync(user, "Administrator"); } }
public void SetConfig(string tenant, string subscription) { var config = new RootUserConfig() { Subscription = subscription, Tenant = tenant }; var updatedConfig = configManager.SaveRootConfig(config); app.Out.WriteLine("Config settings"); app.Out.WriteLine("Tenant: {0}", updatedConfig.Tenant); app.Out.WriteLine("Subscription: {0}", updatedConfig.Subscription); }
// 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.UseSqlite( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity <User>(options => options.SignIn.RequireConfirmedAccount = true) .AddRoles <IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>(); services.AddControllersWithViews(); services.AddRazorPages(); services.AddControllersWithViews(); services.AddRazorPages(); services.Configure <IdentityOptions>(options => { options.Password.RequireDigit = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequiredLength = 8; options.User.RequireUniqueEmail = true; options.Lockout.AllowedForNewUsers = false; options.SignIn.RequireConfirmedAccount = false; options.SignIn.RequireConfirmedEmail = false; options.SignIn.RequireConfirmedPhoneNumber = false; }); services.AddScoped <IApplicationService, ApplicationService>(); services.AddScoped <IUserApplicationService, UserApplicationService>(); services.AddMvc(); RootUserConfig rootUserConfig = Configuration.GetSection("RootUser").Get <RootUserConfig>(); if (rootUserConfig.Create) { CreateRootUser(services.BuildServiceProvider(), rootUserConfig); } }