DirectoryEntry oDE = new DirectoryEntry("LDAP://DC=Test1,DC=Test2,DC=gov,DC=lk"); using (DirectorySearcher ds = new DirectorySearcher(oDE)) { ds.PropertiesToLoad.Add("name"); ds.PropertiesToLoad.Add("userPrincipalName"); ds.Filter = "(&(objectClass=user))"; SearchResultCollection results = ds.FindAll(); foreach (SearchResult result in results) { Console.WriteLine("{0} - {1}", result.Properties["name"][0].ToString(), result.Properties["userPrincipalName"][0].ToString()); } }
On the SearchResultCollection results = ds.FindAll();
line I get an exception:
A referral was returned from the server
Why do I get that exception and what does it mean?
Probably the path you supplied was not correct. Check that.
I would recomment the article Howto: (Almost) Everything In Active Directory via C# which really helped me in the past in dealing with AD.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With