SecurityException不允许请求的注册表访问

时间:2016-01-26 20:05:04

标签: c# wpf windows permissions registry

我在WPF(.NET 4.0)中有一个工具应用程序,需要访问注册表并更改子项值。但是当我尝试在Windows Server 2008 x64中执行x86内置的应用程序时,我收到错误“SecurityException不允许请求注册表访问”。当我在Windows 8 x64中执行相同的应用程序时,应用程序运行正常。

我尝试授予注册表项权限,甚至更改所有者,但这没有用。

应用程序以管理员身份运行,我将此值设置为清单文件:

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

这是更改值的方法:

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

当我将构建更改为AnyCPU时,应用程序会在WinServer 2008 x64上按预期更改值,但在构建为x86时则不会。在我的Windows 8 x64中,它完全适用于x86和x64。 你们有线索吗?

1 个答案:

答案 0 :(得分:2)

因此,尝试从Windows Server 2008上运行的32位应用程序访问显式指定的64位注册表视图位置时发生异常。这是因为在Windows Server 2008上注册表为reflected,导致32位应用程序无法访问的值的64位副本。在更高版本的Windows上,例如在Win8 x64上,注册表值是共享的,因此32位应用程序可以访问这两个视图。

虽然您还没有解释为什么应用程序需要写入Windows Server 2008上的两个物理位置,但我认为这不会对您造成任何问题,因为您可以使用AnyCPU目标构建来更新32位和64位位置,因为你的代码依次显式打开每个视图,所以我认为你不需要32位构建,所以这个问题只是一个好奇心吗?

答案 Issue reading x64 registry keys在这里也可能有所帮助。

相关问题