security exception accessing registry when the program runs as scheduled task

余生长醉 提交于 2019-12-25 04:56:17

问题


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!



来源:https://stackoverflow.com/questions/3987776/security-exception-accessing-registry-when-the-program-runs-as-scheduled-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!