c# - The server is not operational -


this code i'm using connecting ldap

 using (direntry = new directoryentry(string.format("ldap://{0}/{1}", this.host, servername)))         {             direntry.refreshcache();             if (!string.isnullorempty(username))             {                 direntry.username = username;                 direntry.password = password;             }             if (direntry.properties.contains("objectguid"))             {                 byte[] guiddatet = (byte[])direntry.properties["objectguid"].value;                 return new guid(guiddatet);             } 

i "the server not operational" error message when run code.

can please tell me i'm doing wrong. , there anyway replace above code direct ldap query.

you should try breaking separate parts, it's easier manage logic, , easier locate errors occurring. go following approach in situation :

  • create ldapconnection object can set options need
  • setup networkcredential instance administrative username , password
  • bind directory user can issue direct ldap query
  • return searchresultentry can process properties

you have few options accomplish this, i'd try :

//delcare network credential administrative username, password, , active directory domain var credentials = new networkcredential(username, password, domain);  //create directory identifier , connection,  var ldapidentifier = new ldapdirectoryidentifier(servername, port, false, false); var ldapconn = new ldapconnection(ldapidentifier, credentials); 

next, make sure you're setting right authtype particular instance. since you're connecting on port 389, use authtype.basic.

ldapconn.authtype = authtype.basic; 

as had asked, there easy way setup direct ldap query using approach. i'm assuming you're searching samaccountname, can modify needed :

string ldapfilter = "(&(objectcategory=person)(objectclass=user)(&(samaccountname={{useryouaretryingtofind}})))"; 

now have setup search request, , send accordingly :

//send search request our delimited attribute list var getuserrequest = new searchrequest(domain, ldapfilter, searchscope.subtree, attributelist)                                      {sizelimit = 1};  //suppress refferal creation happening during search var searchcontrol = new searchoptionscontrol(searchoption.domainscope); getuserrequest.controls.add(searchcontrol); var userresponse = (searchresponse)ldapconn.sendrequest(getuserrequest);  //this load entry i've located,  searchresultentry resultentry = userresponse.entries[0]; 

that should return user you've queried for, along properties you've put attributelist. in context, attributelist string array (string[]) of property names - in case you'll want add 1 called "objectguid".

as reading properties on searchresultentry, can had :

 if(resultentry.attributes.contains("objectguid"))  {      // stuff here  } 

that should going in right direction.

also, if don't have copy of wireshark, highly suggest download - invaluable in diagnosing connection issues active directory.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -