how to check the username in Active Directory in WPF -
i want check whether username exists in active directory? , depending on need execute code.
i have domain , username.how check whether username exists in active directory without password using directorysearcher.
your process has run under active directory user otherwise should provide active directory user credentials when creating principalcontext. simple code find user username:
var context = new principalcontext(contexttype.domain, "yourdomainhost"); var userinfo = userprincipal.findbyidentity(context, username);
edit:
if need use directory searcher can try method:
bool containsuser(string domain, string username) { string ldapbase = string.format("ldap://{0}", domain); // in case if process not running under ad user use: new directoryentry(ldapbase, "username", "password") using (var entry = new directoryentry(ldapbase)) { using (var searcher = new directorysearcher(entry)) { searcher.filter = string.format("(samaccountname={0})", username); return searcher.findone() != null; } } }
Comments
Post a Comment