Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you know the value of <code>sAMAccountName</code> and need to get <code>IADs</code> of the user you need first <em>find</em> the user in Active Directory by the <code>sAMAccountName</code> and get <code>distinguishedName</code> attribute of the user. You know already how to get <code>IADs</code> by <code>distinguishedName</code>.</p> <p>So you should just follow <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms676882%28v=vs.85%29.aspx#c___example" rel="nofollow">the code</a> from MSDN for example. First you get <code>IDirectorySearch</code> interface of the AD container of <code>defaultNamingContext</code> of <code>"LDAP://rootDSE"</code>.</p> <pre><code>IADs domain; ADsGetObject("LDAP://rootDSE", IADs, domain); </code></pre> <p>Then you use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa746365%28v=vs.85%29.aspx" rel="nofollow">IDirectorySearch::ExecuteSearch</a> to apply search using the filter string:</p> <pre><code>(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName=theName)) </code></pre> <p><strong>Note:</strong> The search filter syntax is described <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475%28v=vs.85%29.aspx" rel="nofollow">here</a>. </p> <pre><code>IDirectorySearch directorySearch = domain as IDirectorySearch; ADS_SEARCH_HANDLE searchHandle; directorySearch.ExecuteSearch( "(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName=ian))", attributeNames, numberOfAttributes, out searchHandle); </code></pre> <ul> <li><p>you use the known value of <code>sAMAccountName</code> instead of <code>theName</code>. </p></li> <li><p>for <code>pAttributeNames</code> you can use <code>LPOLESTR</code> array which consist from <code>L"distinguishedName"</code> only (see <code>pszNonVerboseList</code> from the code example and look the code of <code>FindUsers</code> in case of <code>bIsVerbose</code> as <code>FALSE</code>). </p></li> </ul> <p>You should get <code>distinguishedName</code> attribute of first (and the only if any exist) found item. Having <code>distinguishedName</code> attribute you can use <code>AdsGetObject</code> to get the <code>IADs</code> of the user.</p> <p>Alternatively you can get <code>objectGUID</code> attribute of the user instead of <code>distinguishedName</code> attribute and use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms677985%28v=vs.85%29.aspx" rel="nofollow">binding by GUID</a> syntax, but the usage of <code>distinguishedName</code> I personally find more clear and understandable.</p> <hr> <pre><code>public IADs GetUserObject(string samAccountName) { IADs ads; //Get the current domain's distinguished name (e.g. "dc=stackoverflow,dc=com") AdsGetObject("LDAP://rootDSE", IADs, ref ads); String dn = ads.Get("defaultNamingContext"); //"dc=stackoverflow,dc=com" //Get the the object of the current domain (e.g. LDAP://dc=stackoverflow,dc=com) AdsGetObject("LDAP://"+dn, IADs, ref ads); //Now we're going to search for the "distinguishedName" of this user //setup the search filter for the user we want String filter = "(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName="+samAccountName+"))"; //specify that we only need to return one attribute, distinguishedNamem, //otherwise it returns all attributes and is a waste of resources String[] searchAttributes = { "distinguishedName" }; //run the search IDirectorySearch ds = ads as IDirectorySearch; ADS_SEARCH_HANDLE searchHandle; ds.ExecuteSearch(filter, searchAttributes, 1, out searchHandle); ds.GetFirstRow(searchHandle); //Now get the details of the "distinguishedName" column ADS_SEARCH_COLUMN column; ds.GetColumn(searchHandle, "distinguishedName", ref column); //Get the user's distinguishedName String dn = column.pADsValues.DNString; //Now that we have the user's distinguishedName, we can do what we really wanted: AdsGetObject("LDAP://"+dn, IADs, ads); return ads; } </code></pre> <p>This means that conceptually it can be broken down into two steps: </p> <ul> <li>getting a user's <strong>distinguishedName</strong> from their <code>samAccountName</code></li> <li>fetching the <code>IADs</code> for a <strong>distinguishedName</strong></li> </ul> <p>And splitting the code:</p> <pre><code>public IADs GetUserObject(string samAccountName) { String userDistinguishedName = GetUserDistinguishedName(samAccountName); return GetObject("LDAP://"+userDistingishedName); } public String GetUserDistinguishedName(string samAccountName) { //Get the current domain's distinguished name (e.g. "dc=stackoverflow,dc=com") IADs ads = GetObject("LDAP://rootDSE"); String dn = ads.Get("defaultNamingContext"); //"dc=stackoverflow,dc=com" //Get the the object of the current domain (e.g. LDAP://dc=stackoverflow,dc=com) ads := GetObject("LDAP://"+dn); //Now we're going to search for the "distinguishedName" of this user //setup the search filter for the user we want String filter = '(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName='+samAccountName+'))'; //specify that we only need to return one attribute, distinguishedNamem, //otherwise it returns all attributes and is a waste of resources String[] searchAttributes = { "distinguishedName" }; //run the search IDirectorySearch ds = ads as IDirectorySearch; ADS_SEARCH_HANDLE searchHandle; ds.ExecuteSearch(filter, searchAttributes, 1, out searchHandle); ds.GetFirstRow(searchHandle); //Now get the details of the "distinguishedName" column ADS_SEARCH_COLUMN column; ds.GetColumn(searchHandle, "distinguishedName", ref column); //Get the user's distinguishedName return column.pADsValues.DNString; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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