public async Task <ApplicationUser> CreateAsync(ApplicationUser entity, SystemRoleType role, string password) { ApplicationUser result = null; var user = await _userManager.FindByEmailAsync(entity.Email); if (user != null) { return(result); } var createAsyncResult = await _userManager.CreateAsync(entity, password); if (createAsyncResult.Succeeded) { var roleName = role.ToString(); if (!await _roleManager.RoleExistsAsync(roleName)) { await _roleManager.CreateAsync(new ApplicationIdentityRole(roleName)); } await _userManager.AddToRoleAsync(entity, roleName); result = await _userManager.FindByEmailAsync(entity.Email); } return(result); }
public string BulkAddUsers(ICollection <string> usernames, bool createInNaas, string defaultPassword, SystemRoleType defaultRole, ICollection <FlowNameAndRole> accessFlows, bool isUserActive, NodeVisit visit) { ValidateByRole(visit, SystemRoleType.Admin); if (CollectionUtils.IsNullOrEmpty(usernames)) { throw new ArgumentException("usernames cannot be empty"); } // Create the parameter list: ByIndexOrNameDictionary <string> parameters = new ByIndexOrNameDictionary <string>(true); int i = 0; foreach (string username in usernames) { string key = string.Format("{0}{1}", BulkAddUsersConstants.PARAM_USERNAME_PREFIX, i.ToString()); parameters.Add(key, username); ++i; } parameters.Add(BulkAddUsersConstants.PARAM_CREATE_IN_NAAS, createInNaas.ToString()); if (!string.IsNullOrEmpty(defaultPassword)) { parameters.Add(BulkAddUsersConstants.PARAM_DEFAULT_PASSWORD, defaultPassword); } parameters.Add(BulkAddUsersConstants.PARAM_DEFAULT_ROLE, defaultRole.ToString()); parameters.Add(BulkAddUsersConstants.PARAM_IS_ACTIVE, isUserActive.ToString()); if (!CollectionUtils.IsNullOrEmpty(accessFlows)) { i = 0; foreach (FlowNameAndRole accessFlow in accessFlows) { string key = string.Format("{0}{1}", BulkAddUsersConstants.PARAM_FLOW_NAME_PREFIX, i.ToString()); parameters.Add(key, accessFlow.FlowName + BulkAddUsersConstants.PARAM_VALUE_SEPARATOR + accessFlow.FlowRole); ++i; } } // Create the task transaction and queue for running, the task will run asynchronously after // this method returns return(_transactionManager.QueueTask(_securityFlowName, _securityBulkAddUsersServiceName, visit.Account.Id, parameters)); }
private static async Task <ApplicationUser> AssignUserToRole(UserManager <ApplicationUser> userManager, ApplicationUser user, SystemRoleType role) { var userInDb = await userManager.FindByNameAsync(user.UserName); if (userInDb != null) { return(null); } var result = await userManager.CreateAsync(user, "Admin1."); if (result.Succeeded) { await userManager.AddToRoleAsync(user, role.ToString()); return(userManager.FindByNameAsync(user.UserName).Result); } return(null); }