【问题标题】:Remove user account from administrators group on remote machine using C# and AccountManagment namespace使用 C# 和 AccountManagment 命名空间从远程计算机上的管理员组中删除用户帐户
【发布时间】:2011-10-01 20:59:49
【问题描述】:

我有代码:

 public bool RemoveUserFromAdministratorsGroup(UserPrincipal oUserPrincipal, string computer)
 {
        try
        {
            PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine, computer, null, ContextOptions.Negotiate, _sServiceUser, _sServicePassword);
            GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, "Administrators");

            oGroupPrincipal.Members.Remove(oUserPrincipal);
            oGroupPrincipal.Save();

            return true;
        }
        catch
        {
            return false;
        }

 }

它没有任何例外地工作。但是当我再次运行我的应用程序时,我会在我的列表视图中看到这个用户。因此,该用户并未被删除。

【问题讨论】:

    标签: c# active-directory account-management


    【解决方案1】:

    我已经解决了没有 AccountManagment 命名空间的问题。

     public bool RemoveUserFromAdminGroup(string computerName, string user)
     {
            try
            {
                var de = new DirectoryEntry("WinNT://" + computerName);
                var objGroup = de.Children.Find(Settings.AdministratorsGroup, "group");
    
                foreach (object member in (IEnumerable)objGroup.Invoke("Members"))
                {
                    using (var memberEntry = new DirectoryEntry(member))
                        if (memberEntry.Name == user)
                            objGroup.Invoke("Remove", new[] {memberEntry.Path});
                }
    
                objGroup.CommitChanges();
                objGroup.Dispose();
    
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
     }
    

    【讨论】:

    • 这也是我可以枚举一些机器级组(本地或远程)的唯一方法。当机器级组中存在域级主体时,AccountManagement 名称空间会出现一些问题。例如,将 CORP\myuser 添加到本地 Administrators 组将导致 GroupPrincipal 上的 Members 属性失败。
    【解决方案2】:

    以下解决方案是在Directory Service的帮助下删除用户...

       using System.DirectoryServices
    
      private DeleteUserFromActiveDirectory(DataRow in_Gebruiker)
      {
              DirectoryEntry AD = new DirectoryEntry(strPathActiveDirectory ,
                  strUsername, strPassword)
    
              DirectoryEntry NewUser = 
                  AD.Children.Find("CN=TheUserName", "User");
    
             AD.Children.Remove(NewUser);
             AD.CommitChanges();
             AD.Close();
      }
    

    【讨论】:

    • 此代码被删除位于域用户组中的用户。但我需要从远程机器上的管理员组中删除帐户。
    【解决方案3】:

    我不知道你的问题到底是什么,但这样编码:

    try
    {
      PrincipalContext context = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "passwd");
    
      /* Retreive a user principal
       */
      UserPrincipal user = UserPrincipal.FindByIdentity(context, "user1");
    
      /* Retreive a group principal
       */
      GroupPrincipal adminGroup = GroupPrincipal.FindByIdentity(context, @"dom\Administrateurs");
    
      foreach (Principal p in adminGroup.Members)
      {
        Console.WriteLine(p.Name);
      }
    
      adminGroup.Members.Remove(user);
      adminGroup.Save();
    }
    catch (Exception e)
    {
      Console.WriteLine(e.Message);
    }
    

    给我以下例外:

    Information about the domain could not be retrieved (1355)
    

    挖掘了一下,表明我在不在目标域上的计算机上运行我的代码。当我从服务器本身运行相同的代码时,它可以工作。看来运行这段代码的机器至少要联系目标域的DNS。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多