无法查询 AD(获取 DirectoryServicesCOMException)

Kev*_*eus 5 c# windows asp.net directoryservices windows-server-2008

我正在尝试在 Windows Server 2008 R2(已安装 IIS7)上运行的 ASP.Net (4.0) 应用程序中查询 AD。(作为 2.0 应用程序运行时也会失败)

这对我来说并不新鲜,因为我以前已经做过很多次了。我编写了一个小型 ASP.Net 程序,它在我自己的计算机(带有 IIS6 的 Windows XP)上运行良好,但在 2008 机器上运行时失败。

(结果是您在文本框中看到用户所属的组列表)

(on button_click) 
var userName = txtUserName.Text;

if (userName.Trim().Length == 0)
{
     txtResults.Text = "-- MISSING USER NAME --";
     return;
}

var entry = new DirectoryEntry("LDAP://blah.blah/DC=blah,DC=blah",
                               "cn=acct, dc=blah, dc=blah",
                               "pass");

var search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("memberOf");

var groupsList = new StringBuilder();

var result = search.FindOne();

if (result != null)
{
   int groupCount = result.Properties["memberOf"].Count;

   for (int counter = 0; counter < groupCount; counter++)
   {
           groupsList.Append((string)result.Properties["memberOf"][counter]);
           groupsList.Append("\r\n");
    }
}

txtResults.Text = groupsList.ToString();
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我在 search.FindOne() 上收到以下错误:

System.DirectoryServices.DirectoryServicesCOMException (0x8007203B): A local error has occurred.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindOne()
   at WebApplication1._Default.btnSearch_Click(Object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)

我们对此做了很多研究,并尝试了我们能想到的每一个 IIS7 设置,但到目前为止还没有。有什么线索吗?

Joh*_*ohn 3

将用户名参数从“cn=xxx, dc=yyy, dc=zzz”更改为“域\用户名”