問題描述
從 asp.net Web 應用程序的活動目錄中獲取用戶的全名 (Get user's full name from active directory in asp.net web application)
我正在嘗試從活動目錄中獲取給定用戶的全名。這段代碼可以在我的電腦上運行,但是當我把它放在服務器上時它會拋出異常:
找不到網絡路徑。
代碼是:
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + "domain" + "/" + Environment.UserName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName"].Value;
Response.Write(obVal);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
知道如何修復它以便它也可以在服務器上運行嗎?或者也許我可以通過其他方式獲取給定用戶名的全名?我是否需要以某種方式使用 LDAP?
參考解法
方法 1:
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/
方法 2:
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
(by Greg Oks、Raymund、user2173535)