首页 > 解决方案 > UserPrincipal.FindByIdentity 在 64 位 WinPE 下的 C# 中失败

问题描述

我正在编写一个 64 位 C# Windows 窗体应用程序,它将在 Windows PE 10 64 位下运行以从 Active Directory 中提取显示名称。我必须使用 64 位,因为我运行它的 PC 只会启动 UEFI,因此我无法启动到 32 位恢复环境。每次我的代码到达某个位置时,我都会收到错误消息:未知错误 [0x80005000]。如果我编译它并在 32 位 Windows PE 10 中运行,它运行良好,除了编译 32 位之外无需修改代码。

我使用的是 VS2019,代码使用的是 .NET v4.7。

下面是我正在使用的代码:

    using (PrincipalContext ad = new PrincipalContext(ContextType.Domain, 
        "dotted.domain.com", "OU=Users,DC=dotted,DC=domain,dc=com", 
        ContextOptions.Negotiate, main.interactiveUser, main.interactivePwd))
    {
        try
        {
            //this is where it fails
            using (UserPrincipal wantedUser = UserPrincipal.FindByIdentity(ad, combo1.Text))
            {
                if (wantedUser != null)
                {
                    givenName = wantedUser.DisplayName;
                }
                else
                {
                    MessageBox.Show("User name not found in AD.  Please locate manually", 
                        "Error finding name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    givenName = "DisplayNameNotFoundInAD";
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            break;
        }
    }

以下是我的变量的含义:

dotted.domain.com = 我的域的名称
main.interactiveUser = 使用另一个表单登录域的用户名 main.interactivePwd =
所述用户名的密码
combo1.Text = 组合框中的文本,其中包含我想要的用户名搜索
WantUser = 我想要的人
的用户名 givenName = 我正在搜索的用户名的显示名

我已经尝试过使用和不使用 OU=Users 的结果相同。如果我在 Windows 中正常运行,它也可以正常工作。我发现了一些其他关于可能使用所有者信息编辑注册表的帖子,这没有任何区别。

关于为什么会发生这种情况的任何想法?

标签: c#winpe

解决方案


过去在 64 位 WinPE 中访问注册表时也遇到过类似问题。据我了解,这正是 PrincipalContext 所做的。就我而言,我不得不取消选中

更喜欢 32 位

在 VS 的构建选项卡上。

PS:只会发表评论,但目前我没有足够的声誉


推荐阅读