C# 当前从一天到下一天抛出COMException
今天早上,我开始注意到我的几个程序在Active Directory读取操作方面存在一些问题。我注意到,所有这些应用程序(客户端和服务器)都使用C# 当前从一天到下一天抛出COMException,c#,.net,active-directory,userprincipal,C#,.net,Active Directory,Userprincipal,今天早上,我开始注意到我的几个程序在Active Directory读取操作方面存在一些问题。我注意到,所有这些应用程序(客户端和服务器)都使用System.DirectoryServices.AccountManagement.UserPrincipal类来执行这些读取操作,而程序仍在正确运行时使用System.DirectoryServices.DirectorySearcher 为了解决这个问题,我构建了以下非常简单的控制台应用程序 class Program { static v
System.DirectoryServices.AccountManagement.UserPrincipal
类来执行这些读取操作,而程序仍在正确运行时使用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();
}
}
正如您在评论中所看到的,后两种操作在我测试它的域中的每台计算机上都会失败,尽管它们已经运行了好几年。调用UserPrincipal.Current
或UserPrincipal.FindByIdentity(pc,Environment.UserName)时发生以下异常代码>而不在PrincipalContext中指定容器:
System.Runtime.InteropServices.COMException:“指定的目录服务属性或值不存在”
以下是我所知道的:
- 所有突然停止工作的应用程序都没有在过去两周内收到更新
- 所有这些应用程序,
UserPrincipal.Current
-属性和UserPrincipal.FindByIdentity
-方法昨天运行得非常好
- 工作站在上周没有收到Windows或.Net更新
- 这种现象与单个工作站、用户或操作系统无关,而是发生在运行Windows 7或10的许多不同计算机上的许多不同用户身上
- 域控制器一周前收到更新。显然,其中一个更新有一个关于LDAP查询的已知问题:。这似乎不太可能是突然出现故障的原因,因为a)该修补程序是在一周前安装的,问题只在今天出现;b)Microsoft建议的解决方法(重新启动服务)没有任何效果
如果你知道是什么导致了“一夜之间”的这种行为,请与我分享。如果它真的与Windows更新相关,其他用户很快也会遇到此错误
显然,我可以构建变通方法,因此我不必使用失败的方法和属性,但我仍然需要知道为什么它首先停止工作
编辑:首先,了解两者之间的区别会很有用
public-PrincipalContext(ContextType-ContextType)
和公共PrincipalContext(ContextType、ContextType、字符串名称、字符串容器)代码>。
没有容器构造的PrincipalContext仍然必须以某种方式获得该容器,不是吗
另外,如果你否决了我的问题,我很高兴知道,为什么。否则很难改进;) 默认情况下,PrincipalContext
在“OU=Computers”-容器中搜索。
如果未为容器设置读取权限,则此操作将失败,并将引发COM异常。您是否了解发生COM异常的原因?需要设置哪些权限?