【问题标题】:C# - Cannot use PrincipalContext(ContextType.Machine) on Windows 10: The system cannot find the file specifiedC# - 无法在 Windows 10 上使用 PrincipalContext(ContextType.Machine):系统找不到指定的文件
【发布时间】:2016-01-24 02:02:48
【问题描述】:

以下适用于 Windows 的多个服务器版本,以及 Windows 7 pro 和其他版本,但在 Windows 10(至少两台机器)上我收到错误:“系统找不到指定的文件。\r\n”。错误来源是“Active Directory”

System.IO.FileNotFoundException:系统找不到指定的文件。

堆栈跟踪:

   at System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADs.Get(String bstrName)
   at System.DirectoryServices.AccountManagement.CredentialValidator.BindSam(String target, String userName, String password)
   at System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password)
   at System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password)
   at IsValidWindowsUser(String userName, String password) in MembershipProvider.cs:line xxx

以下是触发错误所需的最少代码量。在这种情况下,我使用的是 ValidateCredentials() 但其他操作(例如 PrincipalSearches)也会失败并出现完全相同的错误。

我已使用 LogonUser() 验证了凭据是否有效; https://msdn.microsoft.com/en-us/library/windows/desktop/aa378184(v=vs.85).aspx

    bool IsValidWindowsUser(string userName, string password)
    {
        using (var p = new PrincipalContext(ContextType.Machine))
            return p.ValidateCredentials(userName, password);
    }

重要提示:我发现这可以在两个 Windows 10 系统上运行,但从那时起,两个系统的硬盘驱动器都被固态驱动器取代。操作系统移动是使用磁盘映像软件完成的,操作系统上的所有其他内容都完好无损。我知道磁盘/分区签名已更改。这会破坏/损坏然后凭证存储吗?一台 Windows 7 pro 计算机也更换了驱动器,但仍能正常工作。

另一个可能相关的老问题是:Validate a users credentials on the local machine

这个问题似乎也谈到了 PrincipalContext(ContextType.Machine) 的 FileNotFoundException 异常,但前提是没有网络连接。就我而言,总是有网络连接。我会使用 LogonUser,但我还需要测试组成员身份。

【问题讨论】:

    标签: c# windows active-directory


    【解决方案1】:

    在 StackOverFlow 上找到了答案.... System.DirectoryServices.AccountManagement.PrincipalContext broken after Windows 10 update

    感谢杜格尔

    相关位...

    该问题是由于缺少注册表项引起的 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion,具体为:RegisteredOwner 和 注册机构

    填充这些键,它应该可以工作。我什至不必重新启动我的应用程序。

    在任何人问之前,是的,我搜索了很多次和很多方式......

    【讨论】:

      【解决方案2】:

      取消选中项目属性窗口中 Build 选项卡下的 Prefer 32-bit 复选框,默认选中 - 参见屏幕截图。这为我修好了!再次选中该复选框将导致您描述的异常重新出现。

      Uncheck 'Prefer 32-bit' screenshot

      【讨论】:

      • 避免问题不是解决方案。这是一个错误,应该修复,请参阅@AnthonyVO 的回答。至于错误在哪里,这是一个争论的焦点,可以说它与 Windows 相关,因为设置错过了值,或者它可能与框架相关,因为它负责交付 API 而不管平台如何。
      猜你喜欢
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多