Get user's full name from active directory in asp.net web application
I am trying to get the full name of a given user from active directory. This code works on my PC but when I put it on the server it throws exception:
The network path was not found.
The code is:
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + "domain" + "/" + Environment.UserName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName开发者_如何学JAVA"].Value;
Response.Write(obVal);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Any idea how to fix so it would work on the server also? Or maybe some other way I can get the full name of a given user name? Do I need to use LDAP instead somehow?
There can be many issues here is what I found out
Your new Directory object is pointing to WINNT I guess you need to use LDAP address for this one i.e.
new DirectoryEntry("LDAP://" + sADServer + "/" + sNewOUPath, sADUser, sADPassword, AuthenticationTypes.Secure);
In your webserver you need to change the user in your App Pool, and that user should have the proper rights to AD
You can also edit your web.config to do
<identity impersonate="true" />
and mage sure that the one that runs the page on the web server has the proper permission to AD
for a full implementation referenc of AD refer to this one http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/
I just faced with this problem too, and I found another solution. In my case I just added ".local" postfix after domain name. I.e. I've done something like this:
DirEntry = new DirectoryEntry("WinNT://" + "domain.local" + "/" + Environment.UserName);
BR, Vladimir
精彩评论