security exception accessing registry when the pro

2019-08-23 14:50发布

问题:

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

回答1:

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...



回答2:

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?



回答3:

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!