private async Task UpdateRolesAsync(string userId, string[] roles, PasswordBoxUserManager manager = null) { if (manager == null) { manager = PasswordBoxUserManager.Create(); } var user = await manager.FindByIdAsync(userId); if (user == null) { throw new ArgumentNullException(nameof(user)); } var userRoles = await manager.GetRolesAsync(user); if (userRoles != null && userRoles.Count > 0) { await manager.RemoveFromRolesAsync(user, userRoles); } if (roles != null) { await manager.AddToRolesAsync(user, roles); } }
public async Task <User> GetByID(string id) { User result = null; using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { result = await manager.FindByIdAsync(id); } return(result); }
public async Task DeleteAsync(string id) { using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { var user = await manager.FindByIdAsync(id); if (user != null) { var result = await manager.DeleteAsync(user); if (!result.Succeeded) { throw new BusinessException(result.Errors.Select(a => a.Description).ToList()); } } } }
public async Task <User> UpdateAsync(User entity, string[] roles = null) { using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { var user = await manager.FindByIdAsync(entity.Id); user = user.Update(entity); var result = await manager.UpdateAsync(user); if (!result.Succeeded) { throw new BusinessException(result.Errors.Select(a => a.Description).ToList()); } await UpdateRolesAsync(entity.Id, roles, manager); } return(entity); }
public async Task <List <string> > GetRolesByUserIdAsync(string id) { if (String.IsNullOrWhiteSpace(id)) { return(new List <string>()); } List <string> result = new List <string>(); using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { var user = await manager.FindByIdAsync(id); if (user != null) { var roles = await manager.GetRolesAsync(user); result = roles.ToList(); } } return(result); }
public async Task <User> Add(User entity, string password, string[] roles = null) { if (String.IsNullOrWhiteSpace(password)) { throw new BusinessException(MessageText.PleaseWritePassword); } using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { IdentityResult result = await manager.CreateAsync(entity, password); if (!result.Succeeded) { throw new BusinessException(result.Errors.Select(a => a.Description).ToList()); } if (roles != null) { await manager.AddToRolesAsync(entity, roles); } } return(entity); }