System.DirectoryServices.AccountManagement.PrincipalContext broken after Windows 10 update
Asked Answered
I

3

34

I've been using this little function without any issue for the past few years to validate user credentials. The createPrincipalContext method returns a PrincipalContext with ContextType.Machine and the machine name.

public static bool ValidateCredentials(string username, string password, string domain = null) {
    try {
        using (var principalContext = createPrincipalContext(username, domain)) {
            username = GetLoginInfo(username).Username;
            // validate the credentials
            if (principalContext.ValidateCredentials(username, password)) {
                //once valid check if account is enabled
                using (UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, username)) {
                    return user.Enabled.GetValueOrDefault(false);
                }
            }
        }
    } catch (PrincipalOperationException e) {
        traceError(e);
    } catch (Exception e) {
        traceError(e);
    }
    return false;
}

My development machine automatically updated to the latest version of Windows 10 this recently, and since then, principalContext.ValidateCredentials has been throwing the following exception.

System.IO.FileNotFoundException: The system cannot find the file specified.

Other than the machine update nothing else was changed. I've spend the last few days searching the net for what may have caused the issue.

Does anyone have any experience in identifying what may have been the cause and if possible, a solution?

Imagination answered 15/11, 2015 at 8:9 Comment(7)
Just to say I have the same problem and I'm currently investigating. Please add an update if you find a solutionPallas
I have used up all my debugging knowledge on this. The error seems to occur in IAds.GetInfo, but that's COM code so there's no source available. Process Monitor doesn't throw up any clues, it doesn't look like there's any attempt to access a file. Happens on two machines for me, I may have to rollback to the previous Windows 10 buildPallas
I gave up trying to figure out what caused the issue and instead tried a workaround. I was debugging using IIS Express under Web > Servers when i got the issue. I created a local site on the machine and changed Servers to Local IIS and pointed the Project Url to it. from there I was able to debug with out getting the FileNotFoundException.Imagination
Weird. I'm already using local IIS so that's not my problem. I'll try recreating the IIS app and see if that fixes itPallas
I believe it is because the dll is not found after the update at C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.DirectoryServices.AccountManagement.dll - not sure what to do with that clue though..Giefer
Has someone reported this to Microsoft?Giefer
That DLL exists for me but I still get the error. I haven't reported it, where's the best place to do it?Pallas
P
50

One final Google before I started rolling back my machine to the previous build and I found this https://connect.microsoft.com/IE/feedback/details/1904887/windows-10-insider-preview-build-10565

the problem is caused by missing registry entries in HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion, specifically: RegisteredOwner and RegisteredOrganization

EDIT: Run the Registry Editor by pressing Windows R and typing regedit.exe. Browse to the location above

Just right click on the CurrentVersion in the Registry Editor and select New > String Value. After you add each entry ( RegisteredOwner and RegisteredOrganization ) edit their values. You can use your username and company name respectively.

Pallas answered 24/11, 2015 at 16:10 Comment(7)
I came across this in my search when the original issue appeared. this answer was not there at the time. I will give it a try. Thanks for directing me back to it.Imagination
Adding the Registry entries solved this for me too. @Imagination Just right click on the CurrentVersion in the Registry Editor and select New > String Value. After you add each entry ( RegisteredOwner and RegisteredOrganization ) edit their values. I used my username and company name respectively.Ghiselin
I had to ask my own question here and poke at the problem multiple times before one final search landed me on this question and answer. Thanks, I was also considering a system rebuild.Dethrone
FYI, the values can be blank. It just needs the value names present.Tremendous
While this answer seems to give the reason for the problem I don't find it very helpful as a solution. Kris' answer below seems to be far more helpful from a developers point of view... (Also they question is are those keys really missing or is a 64Bit application wrongfully accessing 32Bit keys without the developer wanting to do so...)Leaper
For us, the other answer definitely wouldn't work as our app needs to run as 32-bit since it uses some 32-bit COM componentsPallas
I also had this problem and tried the registry changes with no success. The thing that finally fixed it for me was shortening the computer name. It was 13 characters, and worked with a 7 character name instead.Preparatory
B
14

Uncheck the Prefer 32-bit checkbox in your project's properties window under the Build tab, it is checked by default - see screenshot. This fixed it for me! Checking the checkbox again will cause the exceptions you describe to re-appear. I'm guessing this will force it to run in 64-bit mode if possible, and therefore use the 64-bit registry path rather than the WOW6432Node registry path and hence it will find the correct keys it needs.

Uncheck 'Prefer 32-bit' screenshot

Brut answered 17/3, 2016 at 7:18 Comment(3)
I don't get why this has not more votes it is far the better answer than the accepted one! Am I supposed to add random registry keys from within my program everywhere it is started just so that a call that is not even related to the registry works?Leaper
@Leaper Seconded.Tyson
Some applications cannot run as 64bit because they use native 32bit dlls which are not available as 64bit applicationsRecruitment
S
3

Try change your build platform target to "AnyCPU", i found that if my platform target is x86, I have this issue!

Why, yet have no idea, seems like win 10 bug!!!

Susiesuslik answered 24/4, 2016 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.