c# 4.0 - C# Query Active Directory -


i developing intranet web app , using system.directoryservices.accountmanagement query active directory of current user's windows identity. on development machine, query returns userprincipal populated user information. application directory under default web site of local iis on machine has windows authentication , impersonate enabled. however, when application published our hosting iis, principal returned no user information. know why? server admin said have use service account , password connect ad server query. if it's true, querying local machine should have not worked either. correct?

public class ldap_helper {     public string networkname { get; private set; }     public string lastname { get; private set; }     public string firstname { get; private set; }     public string middlename { get; private set; }     public string email { get; private set; }     public string voicephone { get; private set; }      public ldap_helper()     {         using (var context = new system.directoryservices.accountmanagement.principalcontext(             system.directoryservices.accountmanagement.contexttype.domain))         {             try             {                 string currentuser = httpcontext.current.user.identity.name;                 var principal = system.directoryservices.accountmanagement.userprincipal.findbyidentity(context, currentuser);                  networkname = principal.samaccountname;                 lastname = principal.surname;                 firstname = principal.givenname;                 middlename = principal.middlename;                 email = principal.emailaddress;                 voicephone = principal.voicetelephonenumber;             }             catch { }          }          return;     } 

web.config settig:

    <identity impersonate="true" /> 

iis7 authentication iis7 app pool

first off, catch exceptions , nothing them, that's not good. if did handle exception, first principaloperationexception telling no user name found, , 6 nullreferenceexceptions because principal variable nothing. not c# guy, vb bit should not hard translate. should specify domain when create context avoid problems on networks multiple domain controllers. need have permissions query active directory server, no means have service account. regular account appropriate group membership.

 public function finduserprincipal(byval username string) userprincipal     try         return userprincipal.findbyidentity(new principalcontext(contexttype.domain, "mydomain"), identitytype.samaccountname, username)     catch ex principaloperationexception         return nothing     end try end function 

you can narrow search single organizational unit on server specifying search root in form of ldap distinguished name when create context, can improve performance on large networks.

private function getprincipalcontext(byval domain string, byval ldapdn string) principalcontext     try         if string.isnullorwhitespace(ldapdn)             return new principalcontext(contexttype.domain, domain)         else             return new principalcontext(contexttype.domain, domain, ldapdn)         end if     catch ex principaloperationexception         return nothing     end try end function 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -