System.DirectoryServices.AccountManagement.PrincipalContext broken after Windows 10 update

送分小仙女□ 提交于 2019-12-17 17:54:22

问题


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?


回答1:


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.




回答2:


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




回答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!!!



来源:https://stackoverflow.com/questions/33717673/system-directoryservices-accountmanagement-principalcontext-broken-after-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!