title: Active Directory Search to get users domain
body: <p>I am trying to query Active directory for a user to get a list of details:
  1. First/Last Name
  2. Email
  3. UserName
  4. Domain</p>
  
  <p>I am able to get all except for the domain name. Here is my code:</p>
  
  <pre><code>    Dim oroot As DirectoryEntry = New DirectoryEntry("GC://ldap.someCompany.com")
      Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
      Dim result As SearchResult
  
      osearcher.Filter = String.Format("(&amp;(SAMAccountName={0}))", "myUsername")
      osearcher.PropertiesToLoad.Add("cn")
      osearcher.PropertiesToLoad.Add("SAMAccountName")   'Users login name  
      osearcher.PropertiesToLoad.Add("givenName")    'Users first name  
      osearcher.PropertiesToLoad.Add("sn")   'Users sur name  
      osearcher.PropertiesToLoad.Add("mail")   'Email address
  
      result = osearcher.FindOne
  
      Try
          myUser.UserID = result.Properties("cn").Item(0)
          myUser.Domain = ""
          myUser.EmailAddress = result.Properties("mail").Item(0)
          myUser.FirstName = result.Properties("givenName").Item(0)
          myUser.LastName = result.Properties("sn").Item(0)
          myUser.Domain = result.Properties("displayName").Item(0)
  
      Catch ex As Exception
          Return Nothing
      End Try
  </code></pre>
  
  <p>This returns this string: GC://ldap.someCompany.com/CN=FirstName M LastName,OU=Employees,OU=Domain Users,DC=val1,DC=val2,DC=com</p>
  
  <p>So, I have two questions. First, how do I get the domain name of the user? It appears to be in DC= (where I have val1)
  Second, is there anyway to speed this search up? Right now, it takes about 10 seconds to run the query.</p>
  
  <p>I am using .net 4.0 and Visual Studio 2010
  Thanks for any help</p>
  
  <p>jason </p>