views:

29

answers:

3

the following small line throws a System.Security.SecurityException: Requested registry access is not allowed:

RegistryKey _key = HKLM.OpenSubKey("path\\to\\my settings", false);

Now.. what's the point some would ask? The point is that this runs ONLY when I am logged on. The exception is thrown if the program runs as scheduled task and nobody is logged on.

  • the user who runs that task is local administrator
  • the program does not run from a network share, it is located on the local disk
  • I even tried setting Code Access Security
  • the user has the rights to log on as a batch job

I have XP SP3 with all patches applied. The program is written in C# .Net 2.0 (tested 3.5 too)

Does anyone know whats the point here? Torsten

EDIT: see http://gist.github.com/638576

A: 

Mhhhh...it seems related to Authorization problem too. Have you tried to use the API: OpenSubKey(...., RegistryKeyPermissionCheck) to see if something change? I guess it could be related to parent key and its authorization.

Try to see: http://msdn.microsoft.com/it-it/library/microsoft.win32.registrykeypermissioncheck.aspx (in your language). I hope it could help you...

robob
I used:RegistryKey _key = HKLM.OpenSubKey("path\\to\\my settings", RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey);but does not work either
tfl
I should note the full error message - it ends with: The Zone of the assembly that failed was: MyComputer
tfl
try to assign via CAS FullTrust privilege to the content of the Directory where is the EXE of the program. Maybe Computer level of CAS wants a logged user on.
robob
And more try to compile the same assembly for .NET 3.5 and check the Security property of the Visual Studio Project.
robob
A: 

Can you adapt this

WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
string isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator) ? "Yes" : "No";

to check that the process really is successfully impersonating when there's no current user?

Eric Towers
The executing user is admin. It prints "Yes"
tfl
A: 

It seems that this is a problem of this specific computer. I tested it on another workstation and it works even without administrator privileges.

I assumed this - the program did run for years without any problems... Anyway, thanks to all!

tfl