locked
No DistinguishedName? RRS feed

  • Question

  • User-1674175365 posted
    I'm have a function that takes a string (of the value CN) and returns the information about the
    active directory user into several labels. I'm able to get some or all of the information from every user
    except I'm not able to retrieve the DN value. Is is possible that all these Users do not have a Distinguished Name value?

    Here's the function code

    protected void displayCurrentUserInfo(string cn)

    {

    //Label1.Text = dn;

    DirectoryEntry searchRoot = new DirectoryEntry(

    "LDAP://IPAddress/DC=mail,DC=com", //searches will be rooted under this OU

    //"domain\\user", //we will use these credentials

    "DOMAIN\\User",

    "Password",

    AuthenticationTypes.Secure

    );

    using (searchRoot) //we are responsible to Dispose this!

    {

    DirectorySearcher ds = new DirectorySearcher(

    searchRoot,

    "(cn=" + cn + ")", //here is our query

    new string[] { "cn","sn","givenName","mail","telephoneNumber","DN" } //optionally specify attributes to load

    );

    ds.PageSize = 1000; //enable paging for large queries

    using (SearchResultCollection src = ds.FindAll())

    {

    foreach (SearchResult sr in src)

    {

    if (sr.Properties.Contains("givenName"))

    {

    fName.Text = sr.Properties["givenName"][0].ToString();

    }

    else

    {

    fName.Text = "NA";

    }

    if (sr.Properties.Contains("sn"))

    {

    lName.Text = sr.Properties["sn"][0].ToString();

    }

    else

    {

    lName.Text = "NA";

    }

    if (sr.Properties.Contains("mail"))

    {

    eMail.Text = sr.Properties["mail"][0].ToString();

    }

    else

    {

    eMail.Text = "NA";

    }

    if (sr.Properties.Contains("telephoneNumber"))

    {

    pNumber.Text = sr.Properties["telephoneNumber"][0].ToString();

    }

    else

    {

    pNumber.Text = "NA";

    }

    if (sr.Properties.Contains("DN"))

    {

    dnValue.Text = sr.Properties["DN"][0].ToString();

    }

    else

    {

    dnValue.Text = "NA";

    }

    //use the SearchResult here

    }

    }

    }

    }

    Wednesday, February 8, 2006 10:37 AM

All replies

  • User1354132231 posted
    When using the DirectorySearcher.PropertiesToLoad collection, you must ensure you have asked for all the values you wish.

    You have 3 ways of getting the DN:

    1. Add the attribute 'distinguishedName' to the list of attributes to receive (this works for AD & ADAM).
    2. Look for the 'AdsPath' attribute in the SearchResult (it is a quasi-real attribute).
    3. Use the SearchResult.Path property

    The last two give you more on an ADsPath format (includes LDAP://), while the first is a true DN format.

    Wednesday, February 8, 2006 2:50 PM