gpt4 book ai didi

c# - 无法查询 AD(获取 DirectoryServicesCOMException)

转载 作者:可可西里 更新时间:2023-11-01 14:15:35 32 4
gpt4 key购买 nike

我正在尝试在 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();

当我运行这段代码时,我在 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)

我们对此进行了大量研究,并尝试了我们能想到的每一个 IIS7 设置,但还没有到此为止。有什么线索吗?

最佳答案

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

关于c# - 无法查询 AD(获取 DirectoryServicesCOMException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3469883/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com