我正在嘗試創建一個可以根據用戶的Active Directory用戶名查找用戶電子郵件地址的Intranet網站。如何配置ASP.Net網站以訪問Active Directory
我在我的web.config以下:
<authentication mode="Windows"/>
<identity impersonate="true"/>
而且我可以得到該用戶的用戶名:
Environment.UserName
運行在本地主機上,下面的代碼可以讓我查詢公元並獲得電子郵件:
public string GetADUser(string userName)
{
DirectoryEntry entry = new DirectoryEntry();
// get a DirectorySearcher object
DirectorySearcher search = new DirectorySearcher(entry);
// specify the search filter
search.Filter = "(&(objectClass=user)(anr=" + userName + "))";
// specify which property values to return in the search
search.PropertiesToLoad.Add("mail"); // smtp mail address
// perform the search
SearchResult result = search.FindOne();
string email = string.Empty;
if (result != null)
{
if (result.Properties["mail"].Count == 1)
{
email = result.Properties["mail"][0].ToString();
}
else
{
email = "no email";
}
}
else
{
email = "not found";
}
return email;
}
大,這個代碼驗證使用我的憑據由d efault並允許我傳入用戶名並查找用戶的電子郵件地址。
但是,當我將此測試代碼上載到服務器時,如果我從本地主機以外的任何位置瀏覽到該網站,代碼將停止工作。
[COMException (0x80072020): An operations error occurred.]
使用谷歌搜索顯示,我有一個權限問題。
爲了解決這個問題,我嘗試將應用程序池標識設置爲我的憑據,但這仍然不允許代碼搜索AD。
網站身份驗證在IIS中配置如下(標記< <啓用的項目):
Anonymous Authentication:Disabled
ASP.NET Impersonation:Enabled <<
Basic Authentication:Disabled
Digest Authentication:Disabled
Forms Authentication:Disabled
Windows Authentication:Enabled <<
它甚至有可能做什麼,我試圖做的? 我錯過了什麼?
您可能會覺得這很有用:http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C – 5arx 2013-03-11 13:26:20
謝謝,我會讀一讀,看看它是否會幫助。 – philreed 2013-03-11 13:45:13
大量的信息在鏈接中,謝謝。我會保留這些書籤以供將來參考。它沒有直接解決我遇到的問題,但它絕對讓我更好地理解AD。 – philreed 2013-03-11 15:06:02