2015-11-13 60 views
1

我無法從組中刪除用戶。我沒有問題添加用戶。我沒有收到myGroup.Members.Remove(用戶爲DirectoryObject);的任何錯誤。這是一個錯誤嗎?Azure圖api客戶端從組中刪除成員

ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient(); 
     User user = (User)await client.Users.GetByObjectId(userID).ExecuteAsync(); 
     IGroup myIGroup = await client.Groups.GetByObjectId(objectId).ExecuteAsync(); 
     Group myGroup = (Group)myIGroup; 
     if (myGroup != null && user != null) 
     { 
      try 
      { 
       switch (myAction) 
       { 
        case "Delete":        
         myGroup.Members.Remove(user as DirectoryObject); 
         break; 
        case "Add": 
         myGroup.Members.Add(user as DirectoryObject); 
         break; 
       } 
       await myGroup.UpdateAsync(); 

回答

1

嘗試添加Expand(x => x.Members)同時獲得組。我試過以下:

public async Task<Result> RemoveFromGroup(string upn, string groupId) 
{ 
    try 
    { 
     var group = (AD.Group) await ADClient.Groups 
      .Where(x => x.ObjectId == groupId) 
      .Expand(x => x.Members) 
      .ExecuteSingleAsync(); 

     var user = (AD.User) await ADClient.Users 
      .Where(x => x.UserPrincipalName == upn) 
      .ExecuteSingleAsync(); 

     group.Members.Remove(user); 
     await group.UpdateAsync(); 
     return Result.Ok(); 
    } 
    catch (Exception ex) 
    { 
     return Result.Fail(new Error(ex.Message, null, ex)); 
    } 
} 

它的工作原理。唯一的區別 - 我通過其主要名稱獲取用戶,但在這種情況下並不重要。