UserPrincipal.Current从一天到下一个

时间:2017-09-26 15:45:06

标签: c# .net active-directory userprincipal

今天早上我开始注意到有关Active Directory读取操作的几个程序的一些问题。我注意到所有这些应用程序(客户端和服务器)都使用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();
    }
}

正如您在评论中看到的那样,后两个操作将在我测试的域中的每台计算机上失败,即使它们完全工作了好几年。当我在没有在PrincipalContext中指定Container的情况下调用UserPrincipal.CurrentUserPrincipal.FindByIdentity(pc, Environment.UserName);时,会发生以下异常:

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

以下是我所知道的:

  • 突然停止工作的应用程序都没有在过去两周内收到更新
  • 所有这些应用程序,UserPrincipal.Current - 属性和UserPrincipal.FindByIdentity - 方法昨天完美运行
  • 工作站上周未收到Windows或.Net更新
  • 这种现象与单个工作站,用户或操作系统无关,但在许多运行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建议的解决方法(重启服务)没有任何影响

如果你知道可能导致这种行为的原因是什么?#34;过夜",请与我分享。如果它确实与Windows更新有关,其他用户也很快就会遇到这个错误!

我显然可以构建变通方法,因此我不必使用失败的方法和属性,但我仍然必须知道它为什么首先停止工作。

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

另外,如果你对我的问题进行投票,那么知道为什么会非常好。否则很难改进;)

1 个答案:

答案 0 :(得分:2)

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

相关问题