【发布时间】:2013-03-11 11:56:22
【问题描述】:
我正在尝试创建一个 Intranet 网站,该网站可以根据用户的 Active Directory 用户名查找用户的电子邮件地址。
我的 web.config 中有以下内容:
<authentication mode="Windows"/>
<identity impersonate="true"/>
我可以通过以下方式获取用户用户名:
Environment.UserName
在 localhost 上运行,下面的代码可以让我查询 AD 并获取邮件:
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;
}
很好,此代码默认使用我的凭据进行身份验证,并允许我传入用户名并查找用户的电子邮件地址。
但是,当我将此测试代码上传到服务器时,如果我从 localhost 以外的任何地方浏览到该站点,代码就会停止工作。
[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 <<
甚至可以做我想做的事吗? 我错过了什么?
【问题讨论】:
-
您可能会发现这很有用:codeproject.com/Articles/18102/…
-
谢谢,我会读一读,看看是否有帮助。
-
链接中有很多信息,谢谢。我会保留它的书签以供将来参考。它没有直接解决我遇到的问题,但它确实让我对 AD 有了更好的理解。
标签: c# asp.net iis active-directory iis-7.5