我有一个应查询 LDAP 的应用程序,该应用程序拒绝从我的 PC (Windows 11) 运行,但如果我将独立发布输出复制到另一台 PC,它会正常运行并查询 LDAP,不会出现错误。
我得到这个例外:
This exception was originally thrown at this call stack:
System.DirectoryServices.DirectoryEntry.Bind(bool)
System.DirectoryServices.DirectoryEntry.Bind()
System.DirectoryServices.DirectoryEntry.AdsObject.get()
System.DirectoryServices.PropertyValueCollection.PopulateList()
System.DirectoryServices.PropertyValueCollection.PropertyValueCollection(System.DirectoryServices.DirectoryEntry, string)
System.DirectoryServices.PropertyCollection.this[string].get(string)
System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(System.DirectoryServices.DirectoryEntry)
System.DirectoryServices.AccountManagement.ADStoreCtx.ADStoreCtx(System.DirectoryServices.DirectoryEntry, bool, string, string, System.DirectoryServices.AccountManagement.ContextOptions)
System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(System.DirectoryServices.DirectoryEntry)
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
这是我的重现应用程序(netcore7),它显示了异常。原始应用程序是 .NET 4.5 并引发相同的错误,因此这不是网络核心问题:
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
string server = "192.168.80.99";
string container = "CN=Users,DC=mydomain,DC=local";
string connectionString = "LDAP:" + server + "/" + container;
string username = "administrator";
string pwd = "mypassword";
DirectoryEntry de = new DirectoryEntry(connectionString, username, pwd, AuthenticationTypes.Secure);
// Create domain context
ContextOptions options = ContextOptions.Negotiate;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, server, container, options, username, pwd);
// Define a "query-by-example" principal - here, we search for a GroupPrincipal
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
try
{
// Create a principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
// Find all matches
foreach (Principal p in srch.FindAll().Where(g => g.Guid.HasValue).OrderBy(g => g.Name))
{
// Make sure to add only group principals
if (p is GroupPrincipal)
{
Console.WriteLine(p.Name);
//ADGroups.Add(new ADGroupInfo() { Id = p.Guid, Name = p.Name });
}
}
}
catch (System.Security.Authentication.AuthenticationException ex)
{
//ADGroups = null;
Console.WriteLine("GetActiveDirectoryGroups not authenticated. Username:" + username + ". " + ex.Message);
}
Console.WriteLine("Hello, World!");
Console.ReadKey();
在新的 PrimarySearcher 行上引发异常。
我可以从这台 PC 上正常运行“ldp.exe”测试工具并连接到此 AD LDAP 服务器,因此连接性良好。容器路径很好。
我可以使用 Wireshark 并看到它在引发此异常之前登录并传输一堆数据,再次确认连接和身份验证正常。
哦,我也可以成功连接到不同的 LDAP 服务器,而不会抛出此错误。主要区别在于服务器所在的子网/vlan。
有人知道这可能是什么原因造成的吗?
谢谢
7个月后我注意到了这个悬而未决的问题,刚刚粘贴了上面的代码并在同一台PC上完美地连接到同一台服务器。有或没有单个反斜杠。
唯一的区别是时间,因此我认为该问题在 Windows 11/.NET/包含的 AD 库之一中是暂时的。服务器是一个及时冻结的测试虚拟机,因此最终没有任何变化。