0

I have a problem, finding suitable interface to get the user from AD when I have it's LDAP path. I couldn't find anything on the internet, the community generally uses PrincipalSearcher but I'm wondering If you can use LDAP path as parameter.Does anyone have experience with that ?

Kindest Regards.

Davit Karapetyan
  • 589
  • 1
  • 6
  • 14
  • 1
    This looks like it might be what you are looking for http://stackoverflow.com/questions/14813452/connect-to-active-directory-via-ldap – Bearcat9425 Jan 13 '17 at 14:27
  • Don't have time to write an answer out, but I was in need of this same question answered just now. Couldn't find any answers that *actually* answered that question (the answer below is clearly filtering on SAMAccountName, which isn't the same thing). Anyway, the part I was missing: in LDAP, the path is known as the "distinguished name", and distinguishedName is an attribute, which you can then filter on the same way you can filter on any other attribute. – neminem Jul 14 '23 at 18:48

1 Answers1

0

You can use DirectoryEntry for that:

public static bool GetAdUser(string userLoginID, out string userFirstName,
        out string userLastName, out string userEmailAddress)
{
    bool foundUser = false;
    userFirstName = null;
    userLastName = null;
    userEmailAddress = null;

    DirectoryEntry activeDirectory = new DirectoryEntry("LDAP://MyDomainName.local", null, null, 
        AuthenticationTypes.ReadonlyServer);  // supply username and password parameters if needed

    try
    {
        DirectorySearcher adSearcher = new DirectorySearcher(activeDirectory);
        adSearcher.SearchScope = SearchScope.Subtree;
        adSearcher.Filter = string.Format("(&(objectCategory=user)(|(SAMAccountName={0})))", userLoginID);

        string[] adPropertyNames =
        {
            "sn",
            "givenname",
            "mail"
            // add more AD attributes to retrieve here as needed
        };

        foreach (string propertyName in adPropertyNames)
            adSearcher.PropertiesToLoad.Add(propertyName);

        SearchResultCollection userSearchResult = adSearcher.FindAll();

        if (userSearchResult != null)
        {
            foreach (SearchResult adAccount in userSearchResult)
            {
                foundUser = true;
                ResultPropertyCollection accountProperties = adAccount.Properties;

                if (accountProperties != null)
                {
                    userFirstName = GetAdAccountPropertyValue(accountProperties, "givenname");
                    userLastName = GetAdAccountPropertyValue(accountProperties, "sn");
                    userEmailAddress = GetAdAccountPropertyValue(accountProperties, "mail");
                    // add more here as needed
                }
            }
        }
    }
    catch (Exception ex)
    {
        // log something
    }

    return foundUser;
}

private static string GetAdAccountPropertyValue(ResultPropertyCollection adAccountProperties, string propertyName)
{
    string result = null;

    ResultPropertyValueCollection adAccountPropertyValues = adAccountProperties[propertyName];

    if (adAccountPropertyValues != null)
    {
        result = String.Empty;  // property is valid at this point, so initialize its value to empty to show this

        if (adAccountPropertyValues.Count > 0)
        {
            object adAccountPropertyValue = adAccountPropertyValues[0];
            result = adAccountPropertyValue.ToString();
        }
    }

    return result;
}
Tawab Wakil
  • 1,737
  • 18
  • 33