public async Task <IdentityResult> CreateUser(ApplicationUserCreateOptions options) { using (TransactionWrapper.Create(_dbContextIndex["membership"])) { // user var applicationUser = new ApplicationUser { ApplicationId = _applicationUserManager.GetApplicationId(), Email = options.Email, UserName = options.Email, IsApproved = true, EmailConfirmed = true, }; var result = await _applicationUserManager.CreateAsync(applicationUser, options.Password); if (!result.Succeeded) { return(result); } // reload user var user = await _applicationUserManager.FindByEmailAsync(options.Email); // roles if (options.Roles != null && options.Roles.Count > 0) { result = await _applicationUserManager.AddToRolesAsync(user.Id, options.Roles.ToArray()); if (!result.Succeeded) { return(result); } } // claims if (options.Claims != null && options.Claims.Count > 0) { foreach (var claim in options.Claims) { result = await _applicationUserManager.AddClaimAsync(user.Id, claim); if (!result.Succeeded) { return(result); } } } return(result); } }
public async Task <IdentityResult> DeleteUser(Guid id) { var userId = id.ToString(); using (TransactionWrapper.Create(_dbContextIndex["membership"])) { // roles var roles = await _applicationUserManager.GetRolesAsync(userId); var result = await _applicationUserManager.RemoveFromRolesAsync(userId, roles.ToArray()); if (!result.Succeeded) { return(result); } // claims var claims = await _applicationUserManager.GetClaimsAsync(userId); foreach (var claim in claims) { result = await _applicationUserManager.RemoveClaimAsync(userId, claim); if (!result.Succeeded) { return(result); } } // user var user = await GetUser(id); result = await _applicationUserManager.DeleteAsync(user); if (!result.Succeeded) { return(result); } return(result); } }
public async Task <IdentityResult> UpdateUser(ApplicationUserUpdateOptions options) { using (TransactionWrapper.Create(_dbContextIndex["membership"])) { var result = IdentityResult.Success; var userId = options.UserId.ToString(); // password if (!string.IsNullOrEmpty(options.Password)) { var resetToken = await _applicationUserManager.GeneratePasswordResetTokenAsync(userId); result = await _applicationUserManager.ResetPasswordAsync(userId, resetToken, options.Password); if (!result.Succeeded) { return(result); } } // roles if (options.Roles != null) { // remove var roles = await _applicationUserManager.GetRolesAsync(userId); result = await _applicationUserManager.RemoveFromRolesAsync(userId, roles.Except(options.Roles).ToArray()); if (!result.Succeeded) { return(result); } // add result = await _applicationUserManager.AddToRolesAsync(userId, options.Roles.Except(roles).ToArray()); if (!result.Succeeded) { return(result); } } // claims if (options.Claims != null) { // remove var claims = await _applicationUserManager.GetClaimsAsync(userId); foreach (var claim in claims) { result = await _applicationUserManager.RemoveClaimAsync(userId, claim); if (!result.Succeeded) { return(result); } } // add foreach (var claim in options.Claims) { result = await _applicationUserManager.AddClaimAsync(userId, claim); if (!result.Succeeded) { return(result); } } } return(result); } }