如何配置ASP.Net网站以访问Active Directory

时间:2013-03-11 11:56:22

标签: c# asp.net iis active-directory iis-7.5

我正在尝试创建一个Intranet网站,该网站可以根据用户的Active Directory用户名查找用户的电子邮件地址。

我在web.config中有以下内容:

<authentication mode="Windows"/>
<identity impersonate="true"/>

我可以通过以下方式获取用户UserName:

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中配置如下(已启用的项目用&lt;&lt;标记):

Anonymous Authentication:Disabled
ASP.NET Impersonation:Enabled  <<
Basic Authentication:Disabled
Digest Authentication:Disabled
Forms Authentication:Disabled
Windows Authentication:Enabled  <<

甚至可以做我想做的事情吗? 我错过了什么?

1 个答案:

答案 0 :(得分:2)

好的,我发现了问题。

在这种情况下,IIS中的ASP.NET Impersonation:Enabled和我的Web.Config与我配置的应用程序池标识冲突。 (我想)。

我将应用程序池标识设置为使用经过身份验证的相应帐户运行以查询AD,禁用模拟并离开Windows Authentication:Enabled我能够让网站查询AD而不在我的代码中传递任何凭据。

相关问题