【问题标题】:How to configure ASP.Net website to have access to Active Directory如何配置 ASP.Net 网站以访问 Active Directory
【发布时间】: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


【解决方案1】:

好的,我发现了问题。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多