使用 C# 和 AccountManagment 命名空间从远程计算机上的管理员组中删除用户帐户

发布于 2024-12-07 11:07:04 字数 700 浏览 0 评论 0原文

我有代码:

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

 }

它可以正常工作,没有任何解释。但是当我再次运行我的应用程序时,我在列表视图中看到该用户。因此,该用户没有被删除。

I have the code:

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

 }

It is worked without any excaption. But when i run my app again i see this user in my listview. So, the user wasn't removed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居者 2024-12-14 11:07:04

我已经解决了没有 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;
        }
 }

I have solved the issue without AccountManagment namespace.

 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;
        }
 }
夜灵血窟げ 2024-12-14 11:07:04

以下解决方案是在目录服务的帮助下删除用户...

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

The below solution is for deleting the user with the help of 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();
  }
只怪假的太真实 2024-12-14 11:07:04

我不知道你的问题到底是什么,但以这种方式编码:

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。

I don't know what is exactly your problem but coding this way :

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

Give me the following exception :

Information about the domain could not be retrieved (1355)

Digging a bit arround that show me that I was running my code on a computer that was not on the target domain. When I run the same code from the server itself it works. It seems that the machine running this code must at least contact the DNS of the target domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文