2

The following works on several Server versions of Windows, and on Windows 7 pro and others but on Windows 10 (at least two machines) I get the error: "The system cannot find the file specified.\r\n". The error source is "Active Directory"

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

Stack Trace:

   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

The following is the minimal amount of code I need to trigger the error. In this case I am using ValidateCredentials() but other operations such as PrincipalSearches also fail with the exact same error.

I have validated that the credentials are valid using 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);
    }

IMPORTANT BIT: I discovered that this WAS working on both Windows 10 systems but since that point both systems had their harddrives replaced by a solidstate drive. The OS moves were done with Disk Imaging software and everything else on the operating system came across intact. I know that the disk/partition signature changed. Could this have broken/damaged then credential stores? A windows 7 pro computer also had its drive replaced but it continues to work as expected.

Another old question that is possibly related is: Validate a users credentials on the local machine

This question also seems to talk about a FileNotFoundException exception with PrincipalContext(ContextType.Machine) but only if there is no network connection. In my case there is always a network connection. I would use LogonUser but I also need to test group membership.

Community
  • 1
  • 1
AnthonyVO
  • 3,821
  • 1
  • 36
  • 41

2 Answers2

4

And the answer was found on StackOverFlow.... System.DirectoryServices.AccountManagement.PrincipalContext broken after Windows 10 update

Thanks Doogal

The relevant bits...

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

Populate those keys and it should work. I did not even have to restart my application.

Before anyone asks, yes I searched, many times and many ways...

Community
  • 1
  • 1
AnthonyVO
  • 3,821
  • 1
  • 36
  • 41
1

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.

Uncheck 'Prefer 32-bit' screenshot

Kris
  • 179
  • 2
  • 10
  • Avoiding a problem isn't a solution. This is a bug and should be fixed, see @AnthonyVO's answer. As to where the bug is thats a debate, it could be argued that its with Windows because the setup missed the values, or it could be with the framework as it is responsible for delivering the API regardless of platform. – Robert Jul 28 '16 at 10:37