My app needs to impersonate a service account, which I do through a native-call to LogonUser
. However, it appears that random components in the .Net library try to access registry keys the account doesn't have access to, causing a SecurityException
to be thrown.
Specifically, when I load a LinkLabel
, it crashes trying to determine the default hyperlink color in IE:
System.Security.SecurityException: Requested registry access is not allowed. at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource) at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at Microsoft.Win32.RegistryKey.OpenSubKey(String name) at System.Windows.Forms.LinkUtilities.GetIEColor(String name) at System.Windows.Forms.LinkUtilities.get_IELinkColor() at System.Windows.Forms.LinkLabel.get_LinkColor() at System.Windows.Forms.LinkLabel.OnPaint(PaintEventArgs e) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Label.WndProc(Message& m) at System.Windows.Forms.LinkLabel.WndProc(Message& msg) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) The Zone of the assembly that failed was: MyComputer
No, setting the default color does not help.
I found this thread with the exact same problem, but I'm afraid I don't understand the solution:
Registry hives loaded with
LoadUserProfile
are stored under HKU, HKCU remains the interactive logon user's hive (loaded by winlogon.exe).So if you need to get at the newly loaded hive you need to:
- setRegkey
toRegistry.Users
- Open the subkey using the string SID of the user account you are impersonating.
Does anyone know of any workarounds for this?
The problem is you are impersonating too long and your code (indirectly through the .NET framework) is accessing more resources than you intended while impersonating. This exception seems to be caused by the fact your impersonation code is running on a GUI (STA) thread.
You can: