0

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

tfl
  • 1,011
  • 2
  • 12
  • 21

3 Answers3

0

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
  • 1,739
  • 4
  • 26
  • 44
  • I used: RegistryKey _key = HKLM.OpenSubKey("path\\to\\my settings", RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey); but does not work either – tfl Oct 21 '10 at 13:31
  • I should note the full error message - it ends with: The Zone of the assembly that failed was: MyComputer – tfl Oct 21 '10 at 13:33
  • 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 Oct 21 '10 at 15:15
  • And more try to compile the same assembly for .NET 3.5 and check the Security property of the Visual Studio Project. – robob Oct 21 '10 at 15:16
0

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
  • 4,175
  • 1
  • 15
  • 17
0

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
  • 1,011
  • 2
  • 12
  • 21