Get Domain Name from Active Directory

Last post 05-09-2008 8:19 AM by giftsonjohn. 2 replies.

Sort Posts:

  • Get Domain Name from Active Directory

    05-08-2008, 9:53 AM
    • Loading...
    • giftsonjohn
    • Joined on 06-28-2006, 9:35 AM
    • Posts 2

    Hi,

    Can anyone tell me how to get the domain name (i.e.(domain\login) the domain part) from Active Directory?

    I am using DirectorySearcher to search an user in Active Directory Domain. Our Domain has many Sub Domains. After fetching the user from Active Directory, I'm not able to get the domain name.

     

    DirectoryEntry gc;
    DirectoryEntry searchRoot = null;
    DirectorySearcher searcher;
    SearchResultCollection result;
    
    gc = new DirectoryEntry("GC:");
    
    foreach (DirectoryEntry child in gc.Children)
    {
          searchRoot = child;
    }
    
    searcher = new DirectorySearcher(searchRoot, string.Format("(&(objectCategory=person)(objectClass=user)(CN=*{0}*))", textBox1.Text), new string[] { "distinguishedName" }, SearchScope.Subtree);
    searcher.PropertiesToLoad.Add("GivenName");
    searcher.PropertiesToLoad.Add("SN");
    searcher.PropertiesToLoad.Add("mail");
    searcher.PropertiesToLoad.Add("sAMAccountName");
    searcher.PropertiesToLoad.Add("department");
          
    result = searcher.FindAll();
    
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("Department");
    dt.Columns.Add("NTLogin");
    dt.Columns.Add("Email");
    
    foreach (SearchResult oSearchRes in result)
    {
          DataRow dr = dt.NewRow();
          dr[0] = oSearchRes.Properties["GivenName"][0].ToString() + " " + oSearchRes.Properties["SN"][0].ToString();
    
          if (oSearchRes.Properties.Contains("Department"))
              dr[1] = oSearchRes.Properties["Department"][0].ToString();
    
          dr[2] = oSearchRes.Properties["sAMAccountName"][0].ToString();
    
          if (oSearchRes.Properties.Contains("mail"))
               dr[3] = oSearchRes.Properties["Mail"][0].ToString();
      
          dt.Rows.Add(dr);
    }
    
    dataGridView1.DataSource = dt;

    Any Idea how to get the domain name of an user.

     Thanks in Advance.

  • Re: Get Domain Name from Active Directory

    05-08-2008, 10:07 AM

    Use the Environment class.  It has properties for the username, domain name, machine name, etc. 

  • Re: Get Domain Name from Active Directory

    05-09-2008, 8:19 AM
    • Loading...
    • giftsonjohn
    • Joined on 06-28-2006, 9:35 AM
    • Posts 2

    Thanks for your reply.

    I want to get the friendly domain name.

    For example, the active directory is configured as IN.ABC.COM and the employees used to login into the domain by ABC-IN\USER1.

    I can query the active directory and get all the list of users. I can construct the domain IN.ABC.COM from the distinguishedname property. But I don't know how to get the Friendly Domain Name ABC-IN from the Domain.

     Any help is appreciated.

     

    Thanks. 

Page 1 of 1 (3 items)