UserPrincipal.FindByIdentity fails in C# under 64bit WinPE

2

I am writing a 64-bit C# Windows forms application that will run under Windows PE 10 64-bit to pull the Display Name from Active Directory. I have to use 64-bit because the PCs I am running this on will only boot UEFI so I cannot boot into a 32-bit recovery environment. Every time my code reaches a certain spot, I receive the error message: Unknown Error [0x80005000]. If I compile this and run in 32-bit Windows PE 10, it runs fine, with no modification to the code other than compiling 32-bit.

I am using VS2019 and the code is using .NET v4.7.

Below is the code that I am using:

    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;
        }
    }

Here are the meanings of my variables:

dotted.domain.com = the name of my domain
main.interactiveUser = user name to log into the domain with from another form
main.interactivePwd = password for said user name
combo1.Text = text from combo box that has the user name I want to search for
wantedUser = user name of the person I want the display name for
givenName = display name for the user name I am searching for

I have tried it with and without the OU=Users with the same result. It also works fine if I am running in Windows normally. I found some other posts about possibly editing the registry with owner info, that made no difference.

Any ideas on why this is happening?

c#
winpe
asked on Stack Overflow Nov 21, 2019 by IJustToldYou

1 Answer

0

Had a similar issue accessing the registry in 64-bit WinPE in the past. From my understanding it is exactly what PrincipalContext does. In my case, I had to uncheck

prefer 32-bit

on the build-tab in VS.

PS: Would have commented only, but currently I'm not on enough reputation

answered on Stack Overflow Nov 25, 2019 by Mahobo

User contributions licensed under CC BY-SA 3.0