Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET: How to look up a user in Active Directory?
    primarykey
    data
    text
    <p>How do you look up a user in Active Directory?</p> <p>Some example usernames are:</p> <ul> <li>avatopia\ian</li> <li>avatar\ian</li> <li>ian@avatopia.com</li> <li>ian@avatopia.local</li> <li>avatopia.com\ian</li> </ul> <p>It's important to note that i don't know the name of the domain, <a href="https://stackoverflow.com/questions/192366/how-to-grab-ad-credentials-from-client-machine-in-a-web-application#192414">and i shouldn't be hard-coding it</a>.</p> <p>There is some <a href="https://stackoverflow.com/questions/192366/how-to-grab-ad-credentials-from-client-machine-in-a-web-application">sample code on stack-overflow</a> that fails. </p> <pre><code>using System.DirectoryServices; /// &lt;summary&gt; /// Gets the email address, if defined, of a user from Active Directory. /// &lt;/summary&gt; /// &lt;param name="userid"&gt;The userid of the user in question. Make /// sure the domain has been stripped first!&lt;/param&gt; /// &lt;returns&gt;A string containing the user's email address, or null /// if one was not defined or found.&lt;/returns&gt; public static string GetEmail(string userid) { DirectorySearcher searcher; SearchResult result; string email; // Check first if there is a slash in the userid // If there is, domain has not been stripped if (!userid.Contains("\\")) { searcher = new DirectorySearcher(); searcher.Filter = String.Format("(SAMAccountName={0})", userid); searcher.PropertiesToLoad.Add("mail"); result = searcher.FindOne(); if (result != null) { email = result.Properties["mail"][0].ToString(); } } return email; } </code></pre> <p>It specifically ensures that you didn't pass a full username. e.g. </p> <pre><code>Bad: avatopia\ian Bad: avatar\ian Good: ian Good: ian </code></pre> <p>Because you are not allowed to pass the domain, it can't differentiate between the two users</p> <pre><code>ian ian </code></pre> <p>Another guy has <a href="https://stackoverflow.com/questions/161398/finding-a-user-in-active-directory-with-the-login-name">the same question</a> on sackoverflow, but the accepted answer says that you must </p> <blockquote> <p>first locate the naming context for the required domain</p> </blockquote> <p>i <a href="https://stackoverflow.com/search?q=naming+context+active+directory">don't know</a> what a "naming context" is, and i don't know what the "required domain" is. i'd really rather not write a regular expression to try to parse usernames into domain names and account names, e.g. </p> <pre><code>domain.something\user-name </code></pre> <p>into</p> <pre><code>domain.something user-name </code></pre> <p>because i know there will be some edge case that i'll get wrong. i want the proper, intended, method of looking up a user in active directory.</p> <p>There's a nice page on CodeProject <a href="http://www.codeproject.com/KB/system/everythingInAD.aspx" rel="nofollow noreferrer">How to do almost everything in Active Directory</a>, but you can't lookup a user's information by username</p> <p>i would hope that i can give my domain controller (<a href="http://weblogs.asp.net/steveschofield/archive/2004/04/28/121857.aspx" rel="nofollow noreferrer">whoever it is, where ever it is, whatever it's called</a>) a username, and it will figure out which domain that user belongs to, talk to that domain controller, and get the work done.</p>
    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.
 

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