.net - How can I retain connection credentials across calls in System.DirectoryServices? -
i trying connect active directory domain (w2k8r2 dc) in different forest. end, pass credentials following directoryentry constructor:
directoryentry(string path, string username, string password, authenticationtypes authenticationtype)
this , well. would though retain connection somehow , reuse through calls ad not need pass credentials repeatedly. possible somehow?
thanks!
if want control @ connection level, recommend use system.directoryservices.protocol. can reuse ldap connection make different ldap queries. however, programming paradigm different directoryentry
if need use directoryentry
, have store username , password somewhere , pass them directoryentry
objects. write method getdirectoryentry(string dn)
, have method create directoryentry
me correct username , password. doesn't elegant doesn't wrong. if care password being stored in memory in plain text, use securestring store password.
this nothing wrong because directoryentry
maintaining own ldap connection pool. if have multiple directoryentry
same username , password, smart enough share ldap connection. it's same holding single ldap connection , doing different ldap queries. it's not going re-authenticate ldap server each of directoryentry
objects
if don't rely on black box feature directoryentry
, following suggested workaround may make feel better.
static directoryentry getobject(directoryentry root, string dn) { using (directorysearcher searcher = new directorysearcher(root)) { searcher.filter = "(distinguishedname=" + dn + ")"; searcher.searchscope = searchscope.subtree; searchresult result = searcher.findone(); if (result == null) return null; return result.getdirectoryentry(); } }
you need bind root object username , password. then, can keep root object static variable or whatever like. then, directoryentry
object doing ldap query searchroot
set root object. returned directoryentry
still use username , password root. again, not doing better passing in username , password directoryentry
. indeed, performance-wise, it's worse because need 1 more ldap query directoryentry
Comments
Post a Comment