private async Task ExecuteStoreProcedure(string storedProcedure, GroupMember member, Guid groupId) { using (var conn = new SqlConnection(_connectionString)) { await conn.OpenAsync(); using (var cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storedProcedure; cmd.Parameters.AddWithValue("groupId", groupId); cmd.Parameters.AddWithValue("memberId", member.Id); try { await cmd.ExecuteNonQueryAsync(); } catch (SqlException e) { switch (e.Number) { case 50001: throw GroupNotFoundException.Create(groupId, e); case 50002: throw MemberNotFoundException.Create(member.Id, e); } throw; } } } }
private Exception CreateNotFoundException(Guid groupId, Guid? memberId, RemoteException ex) { Exception exception = null; foreach (Match match in guidRegex.Matches(ex.Message)) { Guid? guid = Guid.Parse(match.Groups["guid"].Value); if (guid == groupId) { exception = GroupNotFoundException.Create(groupId, ex); break; } if (guid == memberId) { exception = MemberNotFoundException.Create(groupId, ex); break; } } return exception; }
public async Task RemoveGroupMemberAsync(GroupMember member, Guid groupId) { if (member.MemberType != GroupMemberTypes.OnPremAd) { throw new ArgumentException(nameof(member), "Can only remove members of type 'OnPremAd'"); } string groupDn = GetDistinguishedName(groupId); if (groupDn == null) { throw GroupNotFoundException.Create(groupId); } string memberDn = GetDistinguishedName(member.Id); if (memberDn == null) { throw MemberNotFoundException.Create(member.Id); } using (DirectoryEntry directoryEntry = GetDirectoryEntry(groupDn)) { try { directoryEntry.Properties["member"].Remove(memberDn); directoryEntry.CommitChanges(); } catch (DirectoryServicesCOMException ex) { if ((uint)ex.HResult == LDAP_NO_SUCH_OBJECT) { throw GroupNotFoundException.Create(groupId, ex); } throw; } } await Task.FromResult(0); }