locked
How to get the canonical name of a user in active directory programaticaly RRS feed

  • Question

  • User-456406926 posted

    Hello,

    I need to get the canonical name of a user from my asp.net 2.0 web project,

    Can anyone help?
    Thanks.

    Thursday, May 17, 2007 2:26 AM

Answers

  • User683739415 posted

    Ok.  In order to get anything from the AD you'll need to do the following. 
    (Using C#)

    //Make a DirectoryEntry Object.

    DirectoryEntry de = new DirectoryEntry(LDAP://your.domain);

    //Use this to make a DirectorySearcher Object

    DirectorySearch ds = new DirectorySearcher(de);

    //Specify the account you want with the .Filter on the ds.

    ds.Filter = "sAMAccount = " + username-you-want-the-info-for;

    //Add the properties you want to load.  The Canonical Name is cn I believe.

    ds.PropertiesToLoad.Add("cn");

    //Make a serach result

    SearchResult sr = ds.FindOne();

    //You'll need to make a string to hold the value and set it to the inumerated value.

    string value = sr.Properties["cn"][0].ToString();

     That should about do it. 

     If you want more info I found that this site: http://directoryprogramming.net/default.aspx  was extremely helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 17, 2007 3:05 PM

All replies

  • User683739415 posted

    Ok.  In order to get anything from the AD you'll need to do the following. 
    (Using C#)

    //Make a DirectoryEntry Object.

    DirectoryEntry de = new DirectoryEntry(LDAP://your.domain);

    //Use this to make a DirectorySearcher Object

    DirectorySearch ds = new DirectorySearcher(de);

    //Specify the account you want with the .Filter on the ds.

    ds.Filter = "sAMAccount = " + username-you-want-the-info-for;

    //Add the properties you want to load.  The Canonical Name is cn I believe.

    ds.PropertiesToLoad.Add("cn");

    //Make a serach result

    SearchResult sr = ds.FindOne();

    //You'll need to make a string to hold the value and set it to the inumerated value.

    string value = sr.Properties["cn"][0].ToString();

     That should about do it. 

     If you want more info I found that this site: http://directoryprogramming.net/default.aspx  was extremely helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 17, 2007 3:05 PM
  • User683739415 posted

    Ok.  In order to get anything from the AD you'll need to do the following. 
    (Using C#)

    //Make a DirectoryEntry Object.

    DirectoryEntry de = new DirectoryEntry(LDAP://your.domain);

    //Use this to make a DirectorySearcher Object

    DirectorySearch ds = new DirectorySearcher(de);

    //Specify the account you want with the .Filter on the ds.

    ds.Filter = "sAMAccount = " + username-you-want-the-info-for;

    //Add the properties you want to load.  The Canonical Name is cn I believe.

    ds.PropertiesToLoad.Add("cn");

    //Make a serach result

    SearchResult sr = ds.FindOne();

    //You'll need to make a string to hold the value and set it to the inumerated value.

    string value = sr.Properties["cn"][0].ToString();

     That should about do it. 

     If you want more info I found that this site: http://directoryprogramming.net/default.aspx  was extremely helpful.

    Thursday, May 17, 2007 3:05 PM