2010-01-27 33 views
15

我试图在安装过程中授予对我的应用程序的注册表设置对所有人或所有用户的写入权限。使用.NET设置注册表项写入权限

我的应用程序在安装后没有直接安装适当的权限,无需管理员授予他们即使键和值存在,他们不能更新?我有下面的代码片段,但由于未经授权的访问/访问被拒绝,安装程序失败。我认为我在正确的轨道上...

如何解决权限问题而无需手动注意?有更好的方法吗?我试图用Visual Studio安装程序替换一个额外的安装程序,通过添加此功能。

protected void GrantAllAccessPermission(String key) 
    { 
     try 
     { 
      SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
      NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount; 

      // Get ACL from Windows, allow writing to the registry key 
      using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(key, true)) 
      { 

       RegistrySecurity rs = new RegistrySecurity(); 

       // Creating registry access rule for 'Everyone' NT account 
       RegistryAccessRule rar = new RegistryAccessRule(
        account.ToString(), 
        RegistryRights.FullControl, 
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
        PropagationFlags.None, 
        AccessControlType.Allow); 

       rs.AddAccessRule(rar); 
       rk.SetAccessControl(rs); 
      } 

     } 
     catch (System.Security.SecurityException ex) 
     { 
      throw new InstallException(
       String.Format("An exception in GrantAllAccessPermission, security exception! {0}", key), 
       ex); 
     } 
     catch (UnauthorizedAccessException ex) 
     { 
      throw new InstallException(
       String.Format("An exception in GrantAllAccessPermission, access denied! {0}", key), 
       ex); 
     } 

    } 
+11

我用你的代码片段来完成类似的东西...只是让你知道,'Registry.LocalMachine.OpenSubKey(key)'应该是'Registry.LocalMachine.OpenSubKey(key,true)'(其中'true'表示该键可写)。没有改变,我跑进了'UnauthorizedAccessExceptions'。谢谢! – Pwninstein 2011-03-14 22:10:35

+0

谢谢你,Pwninstein!它为我节省了无数时间的无奈! – 2015-01-21 21:44:37

回答

1

我最终通过切换到Wix 3.0来采取不同的更好的方法。使用Wix安装程序,我可以更轻松地自定义和扩展安装。

添加维克斯的Util扩展命名空间:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" 
    xmlns:util='http://schemas.microsoft.com/wix/UtilExtension'> 

维克斯样品的注册表权限:

<!-- Registry entries --> 
<DirectoryRef Id="TARGETDIR"> 
    <Component Id="RegistryEntries" Guid="{YOUR-GUID}"> 

    <!-- Create registry keys and grant user rights --> 
    <!-- Add Registry Keys and default values as necessary --> 
    <RegistryKey Root="HKLM" Key="$(var.RegKey)" Action="create"> 
     <util:PermissionEx User="[WIX_ACCOUNT_USERS]" GenericAll="yes"/> 
    </RegistryKey> 
    ... 
1

更好的方法是将您的应用程序设置放在您的用户将有权更新的地方。

+1

我同意,但最佳的解决方案需要更大的重构努力。现在我正在尝试改造我继承的应用程序。打击的 – 2010-01-27 23:27:46

+0

。你确定安装程序是以管理/提升权限运行吗? – dkackman 2010-01-28 00:30:44

+0

这是一种可能性,我的开发/测试盒是XP。我会研究一下。 – 2010-01-28 16:36:04

9

我知道这个帖子是有点老,但我想这是值得评论它的任何人可能会像我一样在试图找出类似问题时偶然发现它。你非常接近,我只是改变了两行代码。关键的变化是第一个;当打开密钥时,您需要将其打开为可写。第二个变化是追加新的权限,而不是取代所有的权限......因为你给每个人完全的访问权限,你并不需要这个改变,但是如果你为单个用户添加权限,你会想追加权限。

每一次改变我做了第一次意见了以//改变了旧线:

SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount; 

// Get ACL from Windows 

// CHANGED to open the key as writable: using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(key)) 
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree)) 
{ 

     // CHANGED to add to existing security: RegistrySecurity rs = new RegistrySecurity(); 
    RegistrySecurity rs = rk.GetAccessControl() 

    // Creating registry access rule for 'Everyone' NT account 
    RegistryAccessRule rar = new RegistryAccessRule(
     account.ToString(), 
     RegistryRights.FullControl, 
     InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
     PropagationFlags.None, 
     AccessControlType.Allow); 

    rs.AddAccessRule(rar); 
    rk.SetAccessControl(rs); 
} 
1

试试这个

new System.Security.Permissions.RegistryPermission(System.Security.Permissions.PermissionState.Unrestricted).Assert(); 
try 
{ 
//Your code 
}catch 
{ 
}finally 
{ 
     System.Security.Permissions.RegistryPermission.RevertAssert(); 
}