0

Good day! This is how I search Active Directory

Public Sub UserList(domain As String)
             Try
                      Using context = New PrincipalContext(ContextType.Domain, domain)
              Dim userPrin As New UserPrincipal(context) With {
                  .Enabled = True}
              Using searcher = New PrincipalSearcher(New UserPrincipal(context))
                  searcher.QueryFilter = userPrin

                  For Each result In searcher.FindAll()
                      Dim de As DirectoryEntry = TryCast(result.GetUnderlyingObject(), DirectoryEntry)
                      Dim dn1 As String = de.Properties("cn").Value
                      DataGridView1.Rows.Add(dn1)

                  Next result
              End Using
          End Using
               Catch ex As Exception
          MsgBox(ex.Message)
      End Try
    
  End Sub

Tell me how you can specify, in addition to the domain, also the username and password of the administrator?

This line indicates the domain and the path to the container in which the search is performed

 Using context = New PrincipalContext(ContextType.Domain, domain, OU)
Lider13
  • 47
  • 7
  • 1
    Use this constructor? [PrincipalContext](https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.principalcontext.-ctor?view=dotnet-plat-ext-5.0#System_DirectoryServices_AccountManagement_PrincipalContext__ctor_System_DirectoryServices_AccountManagement_ContextType_System_String_System_String_System_String_System_String_) – Ocaso Protal Aug 24 '21 at 13:13
  • thank you very much. This is all my carelessness. Understood. – Lider13 Aug 24 '21 at 13:18

0 Answers0