C# 通过C删除Active Directory中的用户#

C# 通过C删除Active Directory中的用户#,c#,C#,我正试图通过C#删除Active Directory中的一个用户。当我尝试运行以下代码时,出现了一个错误 错误消息: A local error has occurred 代码: 我有一个运行本地的ASP.Net网站,我们的IT团队使用它来删除广告帐户,它似乎工作正常。我记得在开发这个应用程序时,我必须处理很多细微差别,这会让我很难弄清楚AD到底是怎么回事。下面是我正在使用的代码(在VB.Net中): 看看您的代码,我想到了以下一些想法: 尝试使用dsrc.PropertiesToLoad.A

我正试图通过C#删除Active Directory中的一个用户。当我尝试运行以下代码时,出现了一个错误

错误消息:

A local error has occurred
代码:


我有一个运行本地的ASP.Net网站,我们的IT团队使用它来删除广告帐户,它似乎工作正常。我记得在开发这个应用程序时,我必须处理很多细微差别,这会让我很难弄清楚AD到底是怎么回事。下面是我正在使用的代码(在VB.Net中):

看看您的代码,我想到了以下一些想法:

  • 尝试使用dsrc.PropertiesToLoad.Add(“sAMAccountName”)
  • 尝试添加对ent.CommitChanges()的调用
  • 您可以使用命令行广告查询工具验证路径和凭据是否正确吗
  • 你能确定错误发生在哪一行吗

  • 我有一个运行本地的ASP.Net网站,我们的IT团队使用它来删除广告帐户,它似乎工作正常。我记得在开发这个应用程序时,我必须处理很多细微差别,这会让我很难弄清楚AD到底是怎么回事。下面是我正在使用的代码(在VB.Net中):

    看看您的代码,我想到了以下一些想法:

  • 尝试使用dsrc.PropertiesToLoad.Add(“sAMAccountName”)
  • 尝试添加对ent.CommitChanges()的调用
  • 您可以使用命令行广告查询工具验证路径和凭据是否正确吗
  • 你能确定错误发生在哪一行吗

  • 我同意第二点。我想这就是问题所在。我同意第二点。我想这就是问题所在。看见
    DirectoryEntry ent = new DirectoryEntry("LDAP://192.168.1.99/OU=FIRMA");
        ent.Username = "idm\administrator";
        ent.Password = "123123QQ";
        DirectorySearcher dsrc = new DirectorySearcher(ent);
        dsrc.Filter = string.Format("(&(objectCategory=user)(SAMAccountName=adKullaniciadi))");
        DirectoryEntry silsunuya = ent.Children.Find("CN=adKullaniciadi","objectClass=person");
        ent.Children.Remove(silsunuya);
        ent.Close();
        silsunuya.Close();
        dsrc.Dispose();
    
    Public Shared Function GetUser(ByVal username As String) As DirectoryEntry
        If String.IsNullOrEmpty(username) Then Return Nothing
    
        Dim path As String = ConfigurationManager.ConnectionStrings("ADConnectionString").ConnectionString
        Dim ds As New DirectorySearcher(path)
    
        ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"
        ds.PropertiesToLoad.Add("sAMAccountName")         ' username
        ds.PropertiesToLoad.Add("mail")         ' e-mail address
        ds.PropertiesToLoad.Add("description")  ' Bureau ID
        ds.PropertiesToLoad.Add("company")      ' company name
        ds.PropertiesToLoad.Add("givenname")    ' first name
        ds.PropertiesToLoad.Add("sn")           ' last name
        ds.PropertiesToLoad.Add("name")         ' client name
        ds.PropertiesToLoad.Add("cn")           ' common name
        ds.PropertiesToLoad.Add("dn")           ' display name
        ds.PropertiesToLoad.Add("pwdLastSet")
        ds.SearchScope = SearchScope.Subtree
        Dim results As SearchResult = ds.FindOne
    
        If results IsNot Nothing Then
            Return New DirectoryEntry(results.Path)
        Else
            Return Nothing
        End If
    End Function
    
    Public Shared Sub DeleteUser(ByVal username As String, Optional ByVal useImpersonation As Boolean = False)
        Dim user As DirectoryEntry = GetUser(username)
        Dim ou As DirectoryEntry = user.Parent
        ou.Children.Remove(user)
        ou.CommitChanges()
    End Sub