I must be an idiot programmer without an example. I am migrating a batch file to a asp.net app, using VB, trying to figure out how in asp.net has wasted almost a day.
There is a specific registry key/item I want to change that, by default in Server 2008 and newer, Administrators do NOT have FullControl on. The SetAccessControl function works, as long as I can first set Administrators as the object owner. From the command line it was easy to set the owner as Administrators, then grant FullControl, so I know it is possible.
Edit: the command-line works because I am logged in as a member of the Administrators group, meaning permissions should not be an issue running the code.
Edit2: to be clear, this is a "Windows Forms Application" (NOT a Web Application, NOT a Console Application).
Imports Microsoft.Win32
Imports System.Security.AccessControl
Imports System.Security.Principal
Public Sub GrantRegistyPermission()
Dim Hive As RegistryKey = Registry.ClassesRoot
Dim KeyName As String = "CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
' next line FAILS, "Requested registry access is not allowed"
'Dim SubKey As RegistryKey = Hive.OpenSubKey(KeyName, True)
' try without specifying the "writable" overload
Dim SubKey As RegistryKey = Hive.OpenSubKey(KeyName)
Dim rs As RegistrySecurity = Hive.GetAccessControl()
rs.SetOwner(New NTAccount("BUILTIN\Administrators"))
' next line FAILS, "Attempted to perform an unauthorized operation."
Hive.SetAccessControl(rs)
rs.AddAccessRule(New RegistryAccessRule(User, RegistryRights.FullControl, _
InheritanceFlags.ContainerInherit, _
PropagationFlags.InheritOnly, AccessControlType.Allow))
Hive.SetAccessControl(rs)
Hive.Close()
End Sub