c# - Access all users from active directory -


i want active directory users. have tried following code cn property available. these other properties not returned:

1)username
2)email
3)phoneno

an objectreferance error thrown.

 using (var context = new principalcontext(contexttype.domain, "mydomain.com"))             {                 using (var searcher = new principalsearcher(new userprincipal(context)))                 {                     foreach (var result in searcher.findall())                     {                         directoryentry de = result.getunderlyingobject() directoryentry;                         string str = de.properties["sn"].value.tostring();                     }                 }             } 

   public class users         {              public string email { get; set; } public string username { get; set; } public string displayname { get; set; } public bool ismapped { get; set; }           }     try {      string domainpath = "";     directoryentry searchroot = new directoryentry(domainpath);      directorysearcher search = new directorysearcher(searchroot);     search.filter = "(&(objectclass=user)(objectcategory=person))";     search.propertiestoload.add("samaccountname");     search.propertiestoload.add("mail");     search.propertiestoload.add("usergroup");     search.propertiestoload.add("displayname");      searchresult result;     searchresultcollection resultcol = search.findall();     if (resultcol != null)     {         (int counter = 0; counter < resultcol.count; counter++)         {             string usernameemailstring = string.empty;             result = resultcol[counter];             if (result.properties.contains("samaccountname") &&                       result.properties.contains("mail") &&                  result.properties.contains("displayname"))             {                 users objsurveyusers = new users();                 objsurveyusers.email = (string)result.properties["mail"][0];                 objsurveyusers.username = (string)result.properties["samaccountname"][0];                 objsurveyusers.displayname = (string)result.properties["displayname"][0];                  lstadusers.add(objsurveyusers);             }         }     }  } catch (exception ex) {  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -