安全异常 不允许请求的注册表访问



我在WPF(.NET 4.0)中有一个工具应用程序,需要访问注册表并更改子项值。但是当我尝试在 Windows Server 2008 x64 中执行 x86 中内置的应用程序时,我收到错误"不允许安全异常请求的注册表访问"。当我在 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 中完美运行。你们有什么线索吗?

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

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

答案读取 x64 注册表项的问题也可能在这里有所帮助。

最新更新