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.CurrentUserPrincipal.FindByIdentity(pc, Environment.UserName); 时发生以下异常:

System.Runtime.InteropServices.COMException: “The specified directory service attribute or value does not exist”

这是我所知道的:

如果您知道是什么导致了这种行为 "over night",请与我分享。如果它真的与 Windows 更新有关,其他用户很快也会遇到这个错误!

我显然可以构建解决方法,所以我不必使用失败的方法和属性,但我仍然必须首先知道它停止工作的原因。

编辑: 首先,了解两者之间的区别会很有用 public PrincipalContext(ContextType contextType);public PrincipalContext(ContextType contextType, string name, string container);。 没有容器构造的 PrincipalContext 仍然必须以某种方式获取该容器,不是吗?

此外,如果您对我的问题投反对票,那么很高兴知道为什么。否则很难提高;)

默认情况下 PrincipalContext 在 "OU=Computers"-Container 中搜索。 如果未为 Container 设置读取权限,则会失败,并且会抛出 COM 异常。