Note that there are some explanatory texts on larger screens.

plurals
  1. POQuerying Active Directory with logged on user credentials in c#
    primarykey
    data
    text
    <p>I'm working on a C# application that requires a list of users to be spooled from Active Directory, an AD credential that has read-only privileges has been created but the organization has a policy for not giving a 3rd party user credentials.</p> <p>Judging by <a href="https://stackoverflow.com/questions/4866007/automatic-log-in-to-active-directory/">Nate's answer here</a> it is possible to query AD with the current logged on user. What I would like to know is do I have to preconfigure this in the AppPool Identity or will setting impersonate to "true" in my web.config as shown below solve this?</p> <pre><code>&lt;identity impersonate="true" lockItem="false"/&gt; &lt;anonymousIdentification enabled="true"/&gt; &lt;authentication mode="Windows"&gt; </code></pre> <p>Below is the code I'm using to connect to AD to fetch my list of users:</p> <pre><code>using (PrincipalContext ctx = new PrincipalContext(contextType, domainName)) { try { // define a "query-by-example" principal - here, we search for all users UserPrincipal qbeUser = new UserPrincipal(ctx); qbeUser.Name = "*"; // create your principal searcher passing in the QBE principal PrincipalSearcher srch = new PrincipalSearcher(); srch.QueryFilter = qbeUser; // find all matches foreach (UserPrincipal result in srch.FindAll()) { WindowsUser usr = new WindowsUser(result); // WindowsUser is my own user-defined class resultSet.Add(usr); // resultSet is a list of WindowsUser } } catch (Exception ex) { throw ex; } } return resultSet; </code></pre> <p>Any answers will be greatly appreciated as I have to be at the clients office in a very short space of time.</p>
    singulars
    1. This table or related slice is empty.
    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. 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