Monday, 15 March 2010

c# - search by samaccountname with wildcards -



c# - search by samaccountname with wildcards -

i have code:

public static datatable executesamaccountnamequery(string samaccountname) { string filter = "(&(objectcategory=person)(objectclass=user)(samaccountname=" + samaccountname + "))"; homecoming executeadquery("gc:", filter); }

it works total username, dont know syntax create work wildcards, similar in sql?

thanks

if you're using .net 3.5 or newer, can utilize principalsearcher , "query-by-example" principal searching:

// create domain context principalcontext ctx = new principalcontext(contexttype.domain); // define "query-by-example" principal - here, search userprincipal userprincipal qbeuser = new userprincipal(ctx); qbeuser.samaccountname = "esteban*"; // create principal searcher passing in qbe principal principalsearcher srch = new principalsearcher(qbeuser); // find matches foreach(var found in srch.findall()) { // whatever here - "found" of type "principal" - user, group, computer..... }

if haven't - absolutely read msdn article managing directory security principals in .net framework 3.5 shows nicely how create best utilize of new features in system.directoryservices.accountmanagement. or see msdn documentation on system.directoryservices.accountmanagement namespace.

of course, depending on need, might want specify other properties on "query-by-example" user principal create:

displayname (typically: first name + space + lastly name) sam business relationship name - windows/ad business relationship name user principal name - "username@yourcompany.com" style name

you can specify of properties on userprincipal , utilize "query-by-example" principalsearcher.

c# active-directory

No comments:

Post a Comment