繁体   English   中英

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

[英]How to configure ASP.Net website to have access to Active Directory

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

我的web.config中包含以下内容:

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

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

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;
}

很好,此代码默认情况下使用我的凭据进行身份验证,并允许我传递用户名并查找用户的电子邮件地址。

但是,当我将此测试代码上传到服务器时,如果我从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  <<

甚至有可能做我想做的事情? 我想念什么?

好的,我发现了问题。

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

一旦我将应用程序池标识设置为使用经过身份验证的适当帐户来运行以查询AD,禁用了模拟,并退出了Windows Authentication:Enabled ,就能够使网站查询AD,而无需在代码中传递任何凭据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM