Last post Nov 22, 2011 02:05 PM by gww
Nov 15, 2011 05:31 AM|neharaikar20|LINK
I need to get the email of user logged in from the Active Directory.
This is what i have done -
DirectoryEntry entry = new DirectoryEntry(LDAPpath);
string usermail = " ";
object natobject = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(sAMAccountName=" + name + ")";
SearchResult result = search.FindOne();
if (result == null)
Response.Write("Error finding user!! Please try again");
usermail = (string)result.Properties["mail"];
However, result always comes as Null. The search.filter shows the Active Directory Username properly.
What am i missing here.
Nov 15, 2011 05:48 AM|anurajp|LINK
Try this code
using(DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainController"))
using(DirectorySearcher adSearch = new DirectorySearcher(de))
adSearch.Filter = "(sAMAccountName=someuser)";
SearchResult adSearchResult = adSearch.FindOne();
How to get the current user's Active Directory details in C#
Nov 17, 2011 04:15 AM|kushal.dwivedi|LINK
Code looks ok to me.
just check the name value if it is the correct SamAccountName. Also check the LDAP path if the user exists in the same domain, the LDAP path is reffering to.
You can use any LDAP browins tool to verify the above.
Nov 22, 2011 03:23 AM|Srikanth Kasturi|LINK
Please refer this
Nov 22, 2011 02:05 PM|gww|LINK
Only things I can see would be check to see if the "name" is being passed to the function correctly. I would also make sure you are searching from the root of the AD and not an OU below it. That would limit the search to that OU.
You may also try to provide a username and password in your directoryentry in case its a permission issue. but i would think it would throw an error if you didnt.