0

I need to access a registry key to save the password set by the user. For some reason, RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); throws a SecurityException error. I tried forcing proper permissions with this correction:

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.WriteKey);

but the error still persists. Any suggestions? I would add that this code runs fine when the solution is deployed and run on a full admin user account. Standard user accounts trigger this exception.

Stanley G.
  • 136
  • 11
  • 1
    You don't have access to `HKEY_LOCAL_MACHINE\Software`. –  Dec 27 '16 at 14:16
  • Thank you very much. This solved my problem. – Stanley G. Dec 27 '16 at 15:14
  • Are you sure you want to be storing a password in the registry? Even if you do, make sure it is at least encrypted. – StillLearnin Dec 27 '16 at 20:41
  • @StillLearnin, in clarification of the password use in this scenario, the "password" stored here was actually the product authorization key generated after the master password was entered upon initial installation of the product. The key is an encrypted rendition of the C:\ volume label. – Stanley G. Oct 11 '17 at 23:04

1 Answers1

1

You don't have the permission to write in HKEY_LOCAL_MACHINE as non-Admin user.

Is it necessary for it to be "saved" for the whole computer?

If it's not, you can use

RegistryKey key = Registry.CurrentUser.OpenSubKey("the registry full path");
trinaldi
  • 2,872
  • 2
  • 32
  • 37
R. Pülsinger
  • 165
  • 11