UserPrincipal.Current 从一天到下一天抛出 COMException
UserPrincipal.Current throws COMException from one day to the next
今天早上,我开始注意到我的几个程序在 Active Directory 读取操作方面存在一些问题。我注意到所有这些应用程序(客户端和服务器)都使用 System.DirectoryServices.AccountManagement.UserPrincipal
class 进行那些读取操作,而程序仍然 运行 正确使用 System.DirectoryServices.DirectorySearcher
.
所以为了解决问题,我构建了以下非常简单的控制台应用程序
class Program
{
static void Main(string[] args)
{
//this works great
Console.WriteLine($"Enviroment.Username:{Environment.UserName}");
//this works great
PrincipalContext pcFull = new PrincipalContext(ContextType.Domain, "my.company.de", "dc=my,dc=company,dc=de");
UserPrincipal upPrincipalContextFull = UserPrincipal.FindByIdentity(pcFull, Environment.UserName);
//this doesn't work at all
//Exception: “The specified directory service attribute or value does not exist”
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal upPrincipalContext = UserPrincipal.FindByIdentity(pc, Environment.UserName);
//this doesn't either, same exception
UserPrincipal upCurrent = UserPrincipal.Current;
Console.ReadKey();
}
}
正如您在评论中看到的那样,后两个操作在我测试过的域中的每台计算机上都会失败,即使它们可以完美地工作数年。当我在未在 PrincipalContext 中指定容器的情况下调用 UserPrincipal.Current
或 UserPrincipal.FindByIdentity(pc, Environment.UserName);
时发生以下异常:
System.Runtime.InteropServices.COMException: “The specified directory service attribute or value does not exist”
这是我所知道的:
- none 个突然停止工作的应用程序在过去两周内收到了更新
- 所有这些应用程序,
UserPrincipal.Current
-属性 和 UserPrincipal.FindByIdentity
-方法昨天都运行良好
- 工作站在上周未收到 Windows 或 .Net 更新
- 该现象与单个工作站、用户或 OS 无关,但会发生在许多不同的用户和许多不同的机器上 运行 Windows 7 或10.
- 域控制器一周前收到更新。显然其中一个更新有一个关于 LDAP 查询的已知问题:Due to a defect in WLDAP32.DLL, applications that perform LDAP referral chasing can consume too many dynamic TCP ports。这似乎不太可能是突然失败的原因,因为 a) 该补丁是一周前安装的,但问题是今天才出现的,以及 b) Microsoft 建议的解决方法(重新启动服务)没有任何效果
如果您知道是什么导致了这种行为 "over night",请与我分享。如果它真的与 Windows 更新有关,其他用户很快也会遇到这个错误!
我显然可以构建解决方法,所以我不必使用失败的方法和属性,但我仍然必须首先知道它停止工作的原因。
编辑: 首先,了解两者之间的区别会很有用
public PrincipalContext(ContextType contextType);
和 public PrincipalContext(ContextType contextType, string name, string container);
。
没有容器构造的 PrincipalContext 仍然必须以某种方式获取该容器,不是吗?
此外,如果您对我的问题投反对票,那么很高兴知道为什么。否则很难提高;)
默认情况下 PrincipalContext
在 "OU=Computers"-Container 中搜索。
如果未为 Container 设置读取权限,则会失败,并且会抛出 COM 异常。
今天早上,我开始注意到我的几个程序在 Active Directory 读取操作方面存在一些问题。我注意到所有这些应用程序(客户端和服务器)都使用 System.DirectoryServices.AccountManagement.UserPrincipal
class 进行那些读取操作,而程序仍然 运行 正确使用 System.DirectoryServices.DirectorySearcher
.
所以为了解决问题,我构建了以下非常简单的控制台应用程序
class Program
{
static void Main(string[] args)
{
//this works great
Console.WriteLine($"Enviroment.Username:{Environment.UserName}");
//this works great
PrincipalContext pcFull = new PrincipalContext(ContextType.Domain, "my.company.de", "dc=my,dc=company,dc=de");
UserPrincipal upPrincipalContextFull = UserPrincipal.FindByIdentity(pcFull, Environment.UserName);
//this doesn't work at all
//Exception: “The specified directory service attribute or value does not exist”
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal upPrincipalContext = UserPrincipal.FindByIdentity(pc, Environment.UserName);
//this doesn't either, same exception
UserPrincipal upCurrent = UserPrincipal.Current;
Console.ReadKey();
}
}
正如您在评论中看到的那样,后两个操作在我测试过的域中的每台计算机上都会失败,即使它们可以完美地工作数年。当我在未在 PrincipalContext 中指定容器的情况下调用 UserPrincipal.Current
或 UserPrincipal.FindByIdentity(pc, Environment.UserName);
时发生以下异常:
System.Runtime.InteropServices.COMException: “The specified directory service attribute or value does not exist”
这是我所知道的:
- none 个突然停止工作的应用程序在过去两周内收到了更新
- 所有这些应用程序,
UserPrincipal.Current
-属性 和UserPrincipal.FindByIdentity
-方法昨天都运行良好 - 工作站在上周未收到 Windows 或 .Net 更新
- 该现象与单个工作站、用户或 OS 无关,但会发生在许多不同的用户和许多不同的机器上 运行 Windows 7 或10.
- 域控制器一周前收到更新。显然其中一个更新有一个关于 LDAP 查询的已知问题:Due to a defect in WLDAP32.DLL, applications that perform LDAP referral chasing can consume too many dynamic TCP ports。这似乎不太可能是突然失败的原因,因为 a) 该补丁是一周前安装的,但问题是今天才出现的,以及 b) Microsoft 建议的解决方法(重新启动服务)没有任何效果
如果您知道是什么导致了这种行为 "over night",请与我分享。如果它真的与 Windows 更新有关,其他用户很快也会遇到这个错误!
我显然可以构建解决方法,所以我不必使用失败的方法和属性,但我仍然必须首先知道它停止工作的原因。
编辑: 首先,了解两者之间的区别会很有用
public PrincipalContext(ContextType contextType);
和 public PrincipalContext(ContextType contextType, string name, string container);
。
没有容器构造的 PrincipalContext 仍然必须以某种方式获取该容器,不是吗?
此外,如果您对我的问题投反对票,那么很高兴知道为什么。否则很难提高;)
默认情况下 PrincipalContext
在 "OU=Computers"-Container 中搜索。
如果未为 Container 设置读取权限,则会失败,并且会抛出 COM 异常。