Rafael Kayser Smiderle
Rafael Kayser Smiderle

Reputation: 690

SecurityException Requested registry access is not allowed

I have a tool application in WPF (.NET 4.0) wich needs to access the registry and change a subkey value. But when I try to execute the app built in x86 in a Windows Server 2008 x64, I get the error "SecurityException Requested registry access is not allowed". When I execute the same application in my Windows 8 x64, the application works perfectly.

I tried to give permission for the registry keys and even change the owner, but it's no good.

The application is running as administrator and I set this value to the manifest file:

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>
  <applicationRequestMinimum>
    <defaultAssemblyRequest permissionSetReference="Custom" />
    <PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true" />
  </applicationRequestMinimum>
</security>

This is the method which changes the value:

localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(RegistryHelper.Path64, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);

if (localKey != null)
{
    localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
    localKey.SetValue(RegistryHelper.ProviderKey, provider);
    localKey.Close();
}

localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey = localKey.OpenSubKey(RegistryHelper.Path32, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);

if (localKey != null)
{
    localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
    localKey.SetValue(RegistryHelper.ProviderKey, provider);
    localKey.Close();
}

When I change the build to AnyCPU, the application changes the value as expected on WinServer 2008 x64, but not when built as x86. In my Windows 8 x64 it works in both x86 and x64 perfectly. Do you guys have any clues?

Upvotes: 0

Views: 3449

Answers (1)

jjee
jjee

Reputation: 265

So the exception occurs when trying to access the explicitly specified 64-bit registry view location from the 32-bit application running on Windows Server 2008. This is because on Windows Server 2008 the registry is reflected resulting in the 64-bit copy of the values being inaccessible to 32-bit applications. On later versions of Windows e.g. on Win8 x64 the registry values are shared and so either view is accessible by 32-bit applications.

Although you haven't explained why the application needs to write to both physical locations on Windows Server 2008, i don't think this is going to be a problem for you because you can use an AnyCPU targeted build to update both the 32-bit and 64-bit locations because your code is explicitly opening each view in turn to do so, so i don't think you need a 32-bit build, so this question is just a curiosity right?

The answer to Issue reading x64 registry keys might also help a little here.

Upvotes: 2

Related Questions