web-servicesregistryasmxpage-setup

Accessing the registry from a web service


I've been having difficulties accessing some (but not all) registry keys from my web service. I therefore assumed (and confirmed with some research) that there are some security restrictions on accessing the registry. Is there some code or change in the configuration I need to do specifically in my C#.Net application?

Specifically, I am trying to read and write the values of the PageSetup under "Software\Microsoft\Internet Explorer\PageSetup"


Solution

  • After impersonation of the user HKEY_CURRENT_USER will be not changed. You should use RegOpenCurrentUser after impersonation of the user and RegCloseKey.

    Alternatively you get the user's SID and read registry from HKEY_USERS:

    WindowsIdentity wi = HttpContext.Current.User.Identity as WindowsIdentity;
    if (windowsIdentity != null) {
        SecurityIdentifier si = wi.User;
        RegistryKey key = Registry.Users.OpenSubKey (si.Value +
                                @"\Software\Microsoft\Internet Explorer\PageSetup");
        // get some values which you need like
        string top_margine = key.GetValue ("margin_top");
        key.Close();
    }