c# - 0x80005000 UserPrincipal.GetGroups 出现未知错误,OU 中有特殊字符

标签 c# .net active-directory directoryservices

我正在尝试使用 UserPrincipal 的 GetGroups 方法。如果用户帐户位于包含正斜杠的 OU 中,则对 GetGroups 的调用将失败并出现 COM 未知错误 0x80005000。找到用户帐户就可以找到,我可以访问其他属性。如果我删除 OU 名称中的斜杠,则一切正常。我找到了转义名称中斜杠的引用,但它包含在 GetGroups 方法下。我还发现确保使用我已经完成的 PrincipalContext(ContextType, String) 构造函数。我还尝试使用带有转义斜线的 FQDN 并获得相同的结果。我在 C# 中有一些示例代码:

我使用的是 Visual Studio 2012。代码在 Windows 10 Enterprise x64 上运行。 .net Target版本为4.5

using System;
using System.Linq;
using System.DirectoryServices.AccountManagement;

string SamAccountName = "user1";
//The OUs the user is in:
//Broken OU:  "OU=Test / Test,DC=contoso,DC=com"
//Working OU: "OU=Test & Test,DC=contoso,DC=com"

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, SamAccountName);

//The user was found so this works
Console.WriteLine("User Found: {0}", user.DistinguishedName);

//This causes COM Exception: Unknown Error 0x80005000                
string output = string.Join(Environment.NewLine, user.GetGroups().Select(x => x.Name).ToArray());
Console.WriteLine(output);

最终,我只是替换了 OU 名称中任何这些类型的特殊字符,因为这是迄今为止最简单的解决方案。我主要只是想确保我正在编写的代码不会在未来爆炸。

最佳答案

我认为这是一个错误。

AccountManagement 命名空间的 .NET Core 实现的源代码现已在线提供。我想 .NET Framework 版本大同小异。

我认为问题出在 line 1218 of ADStoreCtx.cs 上:

roots.Add(new DirectoryEntry("GC://" + gc.Name + "/" + p.DistinguishedName, this.credentials != null ? this.credentials.UserName : null, this.credentials != null ? this.credentials.Password : null, this.AuthTypes));

这是将用户的专有名称放入 LDAP 路径中,该路径使用斜杠作为分隔符,而不会转义 DN 中的任何斜杠。

我知道可以在 GitHub 中报告 .NET Core 的错误,但我不确定在哪里报告 .NET Framework 的错误。

关于c# - 0x80005000 UserPrincipal.GetGroups 出现未知错误,OU 中有特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49805255/

相关文章:

.net - 如何设置Word文档的文件名而不从C#和自动化保存它

c# - Active Directory 中的过滤器更改通知 : Create, 删除、取消删除

php - IIS Server 7.0 仅在 Firefox (MacOs) 上返回 401 未经授权的访问

c# - 这个错误是什么意思?远程主机关闭了连接。错误代码为 0x80070057

c# - Linq 中字符(字符串)编码的数字与数字的比较

c# - 依赖注入(inject)中的 Blazor(服务器)作用域对象创建多个实例

.net - .NET 的 Facebook 库

c# - Unity+Firebase-数据库[错误] WebSocket : ws_0 - could not connect

c# - 用于 Windows 开发的 C++ QT 与 C# .NET

powershell - 使用 PowerShell 有效访问 Active Directory 对象