Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to retrieve data from Active Directory by impersonating a Windows authenticated user in ASP.NET?
    primarykey
    data
    text
    <p>I've been trying to solve this problem all day, and I've read some conflicting information within the standard google message board answers.</p> <p>What I'm trying to do is retrieve a domain user's (that is, the currently logged in user's) email address from active directory. My ASP.NET 4 website is setup for Windows Authentication and everything works fine until the active directory calls.</p> <p>When I do the following, I get a COMException on the search.findAll() line. The exception message is "An operations error occured" (Very helpful message eh?) (Stripped down code for readability)</p> <pre><code>WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity; WindowsImpersonationContext wic = null; wic = winId.Impersonate(); using (DirectoryEntry root = new DirectoryEntry(rootQuery)) { String userQuery = GetUserQuery(); DirectorySearcher searcher = new DirectorySearcher(root); searcher.SearchScope = SearchScope.Subtree; searcher.Filter = userQuery; SearchResultCollection results = searcher.FindAll(); return (results[0].Properties["proxyaddresses"][0]).ToString(); } </code></pre> <p>So basically I want to impersonate the logged in user to make the call. Note this code works as expected if I pass in my credentials directly to the DirectoryEntry constructor. Also, I receive the same error if I get rid of the impersonation code and set application wide impersonation in the web config.</p> <p>So I guess my question, before I waste any more time on this, is this even possible? Or do you <em>have</em> to specify a username and password to access AD?</p> <p>BTW on my dev box I'm running IIS5, but will probably deploy to IIS6.</p> <p>edit:</p> <p>as requested:</p> <pre><code>rootQuery = @"LDAP://{0}.com/DC={0}, DC=com"; userQuery = @"(&amp;(samAccountName={0})(objectCategory=person)(objectClass=user))"; </code></pre> <p>with the proper domain and user specified.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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