2011-09-26 3 views
1

Active Directory에서 C#을 통해 사용자를 삭제하려고합니다. 다음 코드를 실행하려고하면 오류가 발생합니다.C#을 통해 Active Directory에서 사용자 삭제

오류 메시지 :

A local error has occurred 

코드 :

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

답변

1

나는 우리의 IT 팀은 AD 계정을 삭제하는 데 사용하는 지역 실행하는 ASP.Net 웹 사이트를 가지고 있고, 확인을 작동하는 것 같다. 나는이 애플리케이션을 개발할 때 뉘앙스가 많았으므로 AD로 무슨 일이 일어나는지 알아내는 것이 고통 스러울 수 있음을 기억합니다. 여기에 내가 (VB.Net에서) 사용하고있는 코드입니다 : "(

  1. dsrc.PropertiesToLoad.Add를 사용해보십시오 :
    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 
    

    이 코드를 보면이 여기에 마음에 와서 몇 가지 아이디어입니다 sAMAccountName ")
  2. ent.CommitChanges()에 대한 호출을 추가하십시오.
  3. 경로 및 자격 증명이 올바른지, 예를 들어 명령 줄 AD 쿼리 도구를 사용하여 확인할 수 있습니까?
  4. 오류가 발생한 행을 구체적으로 결정할 수 있습니까?
+0

나는 2 점에 동의한다. 나는 그것이 문제가 될 것이라고 생각한다. http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.commitchanges.aspx를 참조하십시오. –

관련 문제