public async Task <AppIdentityUserDTO> CreatePartnerAsync(CreateAppIdentityUserDTO create) { // take the appsetting value to role and asign to a var string role = _configuration["UserRoles:Partner"]; // if user send some role, change the object value to null, cero, nothing create.RoleName = ""; #region Convert to Identity class var user = new AppIdentityUser { FullName = create.FullName, Email = create.Email, UserName = create.Email, PhoneNumber = create.Phone, // add default role Role = role }; #endregion var result = await _userManager.CreateAsync(user, create.Password); if (!result.Succeeded) { throw new ArgumentNullException(nameof(result)); } // find the user to add role and return the id var userCreated = await _userManager.FindByEmailAsync(create.Email); // add user to IdentityUserRole var addUserToRole = await _userManager.AddToRoleAsync(userCreated, role); #region Convert to DTO var userInfo = new AppIdentityUserDTO { Id = userCreated.Id, FullName = userCreated.UserName, Email = create.Email, Role = role }; #endregion return(userInfo); }
public async Task <ActionResult> CreateUser([FromBody] CreateAppIdentityUserDTO create) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var confirm = await _identityUserRep.ConfirmEmailAsync(create.Email); if (!confirm) { return(BadRequest("Email is used")); } var result = await _identityUserRep.CreateUserAsync(create); if (result == null) { return(BadRequest("Some error ocurred while try create your account")); } return(Ok(result)); }