3

私はコードを持っています:

 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;
        }

 }

抜粋なしで動作します。しかし、アプリを再度実行すると、リストビューにこのユーザーが表示されます。したがって、ユーザーは削除されませんでした。

4

3 に答える 3

2

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;
        }
 }
于 2011-10-10T17:08:22.370 に答える
0

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();
  }
于 2011-10-02T09:32:16.163 に答える
0

私は正確にあなたの問題が何であるかわかりませんが、このようにコーディングしています:

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 に接続する必要があるようです。

于 2011-10-02T19:14:37.590 に答える