c# - Active Directory Get List of Users using PrincipalSearcher throws error -
i have 2 functionality need achieve in asp.net web application.
- authenticate using active directory
- get list of active directory users.
both above work fine on client environment when set iis application pool network service. when change iis user authentication works list of users stops working. not sure why happening.
here code:
authentication:
public bool authenticateaduser(string domain, string password, string username) { bool isvalid = false; using (principalcontext pc = new principalcontext(contexttype.domain, domain)) { isvalid = pc.validatecredentials(username, password); } return isvalid; }
get list of active directory users:
using (var context = new principalcontext(contexttype.domain, domain)) { if (groupname != string.empty) { // list of users group groupprincipal group = groupprincipal.findbyidentity(context, groupname); foreach (principal principal in group.members) { adusers.add(principal); } } else // users regardless of group { using (var searcher = new principalsearcher(new userprincipal(context))) { foreach (principal principal in searcher.findall()) { if (principal.userprincipalname != null) adusers.add(principal); } } //end principalsearcher using } //end else } //end principalcontext using
updated: can't line of code because deployed application client server, can't debug it. here error:
exception information:
exception type: directoryservicescomexception exception message: logon failure: unknown user name or bad password. @ system.directoryservices.directoryentry.bind(boolean throwiffail) @ system.directoryservices.directoryentry.bind() @ system.directoryservices.directoryentry.get_adsobject() @ system.directoryservices.propertyvaluecollection.populatelist() @ system.directoryservices.propertyvaluecollection..ctor(directoryentry entry, string propertyname) @ system.directoryservices.propertycollection.get_item(string propertyname) @ system.directoryservices.accountmanagement.principalcontext.doldapdirectoryinitnocontainer() @ system.directoryservices.accountmanagement.principalcontext.dodomaininit() @ system.directoryservices.accountmanagement.principalcontext.initialize() @ system.directoryservices.accountmanagement.principalcontext.get_queryctx() @ system.directoryservices.accountmanagement.principalsearcher.setdefaultpagesizeforcontext() @ system.directoryservices.accountmanagement.principalsearcher..ctor(principal queryfilter) @ warshawgroup.onevoice.user.bus_cuser.getadusers(string domain, string groupname) @ warshawgroup.onevoice.ui.data.users.modify.page_load(object sender, eventargs e) @ system.web.util.callieventhandlerdelegateproxy.callback(object sender, eventargs e) @ system.web.ui.control.onload(eventargs e) @ system.web.ui.control.loadrecursive() @ system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint)
updated:
it throws exception @
var searcher = new principalsearcher(new userprincipal(context));
context principalcontext type...
var context = new principalcontext(contexttype.domain, domain);
principalcontext has property called "connectedserver". value connectedserver null, throws exception.
if pass user name , password along domain in principalcontext, works.example...
var context = new principalcontext(contexttype.domain, domain,"username","pwd");
it seems need impersonate user has permission access particular domain.
is there other way can achieve this?
thanks,
Comments
Post a Comment