c# - UserPrincipal.Current 从一天到下一天抛出 COMException

标签 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();
    }
}

正如您在评论中看到的那样,后两个操作在我测试过的域中的每台计算机上都会失败,即使它们可以完美运行数年。当我调用 UserPrincipal.CurrentUserPrincipal.FindByIdentity(pc, Environment.UserName); 而未在 PrincipalContext 中指定容器时,会发生以下异常:

System.Runtime.InteropServices.COMException:“指定的目录服务属性或值不存在”

这是我知道的:

  • 所有突然停止工作的应用程序都没有在过去两周内收到更新
  • 所有这些应用程序、UserPrincipal.Current-Property 和 UserPrincipal.FindByIdentity-Method 昨天都运行良好
  • 工作站在上周没有收到 Windows 或 .Net 更新
  • 该现象与单个工作站、用户或操作系统无关,而是发生在运行 Windows 7 或 10 的许多不同计算机上的许多不同用户身上。
  • 域 Controller 在一周前收到了更新。显然其中一个更新有一个关于 LDAP 查询的已知问题:Due to a defect in WLDAP32.DLL, applications that perform LDAP referral chasing can consume too many dynamic TCP ports .这似乎不太可能是突然失败的原因,因为 a) 该补丁是一周前安装的,但问题是今天才出现的,并且 b) Microsoft 建议的解决方法(重新启动服务)没有任何效果

什么可能会“一夜之间”导致这种行为?如果它真的与 Windows 更新有关,其他用户很快也会遇到这个错误!

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

编辑

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

最佳答案

默认情况下,PrincipalContext 在“OU=Computers”-Container 中搜索。 如果未为容器设置读取权限,则此操作将失败,并将抛出 COM 异常。

关于c# - UserPrincipal.Current 从一天到下一天抛出 COMException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46430779/

相关文章:

c# - 如何在 Xamarin 中使用数据库项填充 ListView?

mysql - 在 .NET Core 2、Mac OS、使用 MySQL 的 Visual Studio for Mac 上 : ArgumentOutOfRangeException: Length cannot be less than zero

.net - 默认实例的连接字符串,例如命名实例

linux - 如何在windows和linux之间共享网络

active-directory - 如何将 Microsoft ADAM 配置为类似于 Active Directory?

C# 套接字 : What if BeginSend is called before BeginReceive?

c# - 如何通过C#代码检测是否安装了Crystal Reports Basic

c# - 使用简短的初始化语法来初始化对值列表

c# - 如何从一个客户端使用多个 WCF 服务

active-directory - Active Directory 中的 userPrincipalName 是否有最大长度?