Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to query ActiveDirectory from native Win32 (i.e. not .NET) code
    primarykey
    data
    text
    <p>If i wanted to pull information about a user from Active Directory in .NET, i could use the <code>DirectorySearcher</code> class. </p> <p>For example, to find the <em>e-mail address</em> of a user i would call:</p> <pre><code>public String GetUserEmailAddress(String accountName) { DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = String.Format("(&amp;(objectCategory=user)(sAMAccountName={0}))", accountName); searcher.PropertiesToLoad.Add("mail"); SearchResult searchResult = searcher.FindOne(); return searchResult.Properties["mail"][0]; } </code></pre> <p>What is the <em>native</em> way to query the Active Directory?</p> <p><strong>Note</strong>: </p> <ul> <li>no domain name is specified</li> <li>no server name is specified</li> </ul> <hr> <p>We can even extend our function to allow querying of any <strong>generic</strong> arbitrary information:</p> <pre><code>public Object GetUserAttribute(String accountName, String propertyName) { DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = String.Format("(&amp;(objectCategory=user)(sAMAccountName={0}))", accountName); searcher.PropertiesToLoad.Add(propertyName); SearchResult searchResult = searcher.FindOne(); return searchResult.Properties[propertyName][0]; } </code></pre> <p>AD has all kinds of information that you can pass as <code>propertyName</code>. For example:</p> <ul> <li><code>displayName</code> (Display-Name): The display name for an object. This is usually the combination of the users first name, middle initial, and last name. (e.g. <strong>Ian A. Boyd</strong>)</li> <li><code>mail</code> (E-mail-Addresses): The list of email addresses for a contact. (e.g. <strong>ianboyd@stackoverflow.com</strong>)</li> <li><code>cn</code> (Common-Name): The name that represents an object. Used to perform searches.</li> <li><code>name</code> (RDN): The Relative Distinguished Name of an object. (e.g. <strong>Ian Boyd</strong>)</li> <li><code>sn</code> (Surname): This attribute contains the family or last name for a user.</li> <li><code>givenName</code> (Given-Name): Contains the given name (first name) of the user.</li> <li><code>sAMAccountName</code> (SAM-Account-Name): The logon name used to support clients and servers running older versions of the operating system, such as Windows NT 4.0, Windows 95, Windows 98, and LAN Manager. This attribute must be less than 20 characters to support older clients.</li> <li><code>objectGUID</code> (Object-Guid): The unique identifier for an object. (e.g. <strong>{3BF66482-3561-49a8-84A6-771C70532F25}</strong>)</li> <li><code>employeeID</code> (Employee-ID): The ID of an employee. /// "description" (Description): Contains the description to display for an object. This value is treated as single-valued by the system.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload