public async Task <ActionResult> AddRootUser([FromBody] AccountBindingModel model) { if (ModelState.IsValid) { var checkOrg = await _uow.OrganisationAccount.FindOneAsync(o => o.Name == model.Name); if (checkOrg != null) { return(BadRequest("Organisation Account already exists")); } var orgAccount = new OrganisationAccount() { Name = model.Name, Region = model.Region, Data = model.Data, AddedBy = model.AddedBy, AddedDate = DateTime.UtcNow }; var resultOrg = _uow.OrganisationAccount.Add(orgAccount); var user = new ApplicationUser { UserName = model.User.Email, Email = model.User.Email, Civility = model.User.Civility, FirstName = model.User.FirstName, LastName = model.User.LastName, IsActive = model.User.IsActive, OrganisationId = resultOrg.Id, IsPasswordTemporary = model.User.IsPasswordTemporary, DateAdded = DateTime.UtcNow, DateModified = DateTime.UtcNow }; var result = await _userManager.CreateAsync(user, model.User.Password); var addedUser = await _userManager.FindByEmailAsync(user.Email); var roleTester = await _userManager.IsInRoleAsync(addedUser, model.User.UserRole); if (!await _userManager.IsInRoleAsync(addedUser, model.User.UserRole)) { result = await _userManager.AddToRoleAsync(addedUser, model.User.UserRole); } if (result.Succeeded) { //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); //await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); } return(Ok(addedUser)); } return(BadRequest()); }
public async Task SeedAuthDatabase(IServiceProvider serviceProvider) { var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); try { if (roleManager.Roles.Any() == false) { await roleManager.CreateAsync(new IdentityRole(DomainRoles.SuperAdmin)); await roleManager.CreateAsync(new IdentityRole(DomainRoles.Admin)); await roleManager.CreateAsync(new IdentityRole(DomainRoles.RootUser)); await roleManager.CreateAsync(new IdentityRole(DomainRoles.Doctor)); await roleManager.CreateAsync(new IdentityRole(DomainRoles.Centre)); } } catch (Exception ex) { _logger.LogError(ex, "Failed to seed authentication database."); } var dbContext = serviceProvider.GetRequiredService <IdentityContext>(); var organisationAccount = new OrganisationAccount() { Name = "Straxcorp PTY Ltd", Region = "AU", Data = "", AddedDate = DateTime.UtcNow, AddedBy = "0" }; if (!dbContext.OrganisationAccount.Any(u => u.Name == organisationAccount.Name)) { try { var result = await dbContext.AddAsync <OrganisationAccount>(organisationAccount); await dbContext.SaveChangesAsync(); } catch (Exception ex) { _logger.LogError(ex, "Failed to seed authentication database."); } } var userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); try { var orgnisationAccount = dbContext.OrganisationAccount.FirstOrDefault(); var adminUser = new ApplicationUser { Civility = "Mr.", FirstName = "Admin", LastName = "Straxcorp", OrganisationId = "", IsActive = true, LastLoggedOn = System.DateTime.Now, ApprovedBy = null, DateAdded = DateTime.Now, DateModified = DateTime.Now, Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", UserName = "******", NormalizedUserName = "******", PhoneNumber = "+61123659863", EmailConfirmed = true, PhoneNumberConfirmed = true, SecurityStamp = Guid.NewGuid().ToString("D"), IsPasswordTemporary = false, OrganisationAccount = orgnisationAccount }; if (await userManager.FindByEmailAsync(adminUser.Email) == null) { var password = new PasswordHasher <ApplicationUser>(); var hashed = password.HashPassword(adminUser, "Straximages#2017"); adminUser.PasswordHash = hashed; var user = await userManager.CreateAsync(adminUser); if (user.Succeeded) { var addToRole = await userManager.AddToRoleAsync(adminUser, DomainRoles.SuperAdmin); } } } catch (Exception ex) { _logger.LogError(ex, "Failed to seed authentication database."); } var configurationContext = serviceProvider.GetRequiredService <ConfigurationDbContext>(); try { //Seeding IDENTITY SERVER Configuration data var _domainSettings = new DomainSettings() { Client = new WebResource() { Id = "oidc_web", Name = "Mvc Client", Secret = "secret", Url = "http://localhost:5002" }, Api = new WebResource() { Id = "auth_api", Name = "Auth API", Secret = "secret", Url = "http://localhost:5001" }, Auth = new WebResource { Id = "", Secret = "", Url = "http://localhost:5000" } }; if (!configurationContext.IdentityResources.Any()) { foreach (var resource in Resources.GetIdentityResources()) { configurationContext.IdentityResources.Add(resource.ToEntity()); } configurationContext.SaveChanges(); } if (!configurationContext.ApiResources.Any()) { foreach (var resource in Resources.GetApis(_domainSettings.Api)) { configurationContext.ApiResources.Add(resource.ToEntity()); } configurationContext.SaveChanges(); } foreach (var client in Resources.OidcMvcClients(_domainSettings)) { var result = configurationContext.Clients.Where(c => c.ClientId == client.ClientId).FirstOrDefault(); if (result == null) { configurationContext.Clients.Add(client.ToEntity()); configurationContext.SaveChanges(); } } } catch (Exception ex) { _logger.LogError(ex, "Failed to seed authentication database."); } }