How to write in registry from ASP.NET App in IIS? - c#

I try to write in the Registry.LocalMachine from an asp.net app. I use the following code, and it works on my development machine :
string value = "some value";
RegistryKey clefDeRegistre = Registry.LocalMachine;
RegistryKey clefTapi = clefDeRegistre.OpenSubKey(#"SOFTWARE\SomeEditor\MySubKey", true);
if (clefTapi != null)
{
clefTapi.SetValue("Options", value);
}
But when I try this on my server (Windows Server 2008 R2), it doesn't work.
With the default app pool settings, I received a security exception when trying to write in the registry. So I changed the app pool identity to LocalSystem.
Now I don't receive any exception, but the key isn't modified.
It looks like a problem with Registry Virtualization, but I don't understand how I can disable it.

If your application pool is in 32 bit mode, then the key goes to SOFTWARE\Wow6432Node branch.

Related

writing to Windows registry from intranet app

I have an intranet web application where encrypted connection string is stored in Windows registry (connection string is shared by some other apps). Application is hosted on a Windows 2012 server. In development/test environments (not production) I want to be able to modify this key so I can switch to different databases.
I do not seem to be able to write to it, I keep getting different errors depending on how I approach it:
string sKeyValue = "SOFTWARE\\SomeProject\\SomeApp";
RegistryKey rk;
rk = Registry.LocalMachine.OpenSubKey(sKeyValue, true); // Requested registry access is not allowed.
Tried:
RegistryPermission p = new RegistryPermission(RegistryPermissionAccess.AllAccess, sKeyValue);
p.Assert();
also
RegistrySecurity rs = new RegistrySecurity();
RegistryKey key = Registry.LocalMachine;
rs = key.GetAccessControl();
string sUser = #"domain\userid";
rs.AddAccessRule(new RegistryAccessRule(sUser, RegistryRights.WriteKey, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
key.SetAccessControl(rs); //Exception: "Attempted to perform an unauthorized operation."
Allowing write access to the registry from a web app is just a bad idea all around. Automatically setting permissions via web code is dangerous and requires the code to have too-elevated privileges.
It's better to give all your web apps permission to read from the same web.config file, then get the shared setting something like what is described here.
IIS web.config - same file for multiple websites

Sharing app.config

I've got 2 applications:
Windows Service (with HTTP listener for config website & api)
Control Utility (WPF app)
The requirements of the control utility are pretty straightforward:
Start / stop service
Launch browser pointing to website (e.g. http://local:5555)
Looking for a Windows Service called "MyService", retrieve its status and start it when needed is pretty simple. However, how do I launch the browser with the correct link? The port which the HTTP listener is listening for is configurable inside the app.config of my Windows Service application and there is no possibility to discover the listener. Can a app.config be shared between 2 applications?
I know ConfigurationManager has OpenExeConfiguration(), but this causes other problems:
I have to know the path where the Windows Service is installed
Reading the configuration may cause a read lock
If the config file is encrypted, I have to know the key
Are there any other solutions to achieve this?
You could use the registry for exchanging data.
// Create a Subkey
RegistryKey newKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\SuperSoft\\App");
// Write Values to the Subkey
newKey.SetValue("Value1", "Content1");
newKey.SetValue("Value2", "Content2");
// read Values from the Subkey
if (SubKeyExist("SOFTWARE\\SuperSoft\\App"))
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\SuperSoft\\App");
string firstApp = (string)myKey.GetValue("Value1");
string secondApp = (string)myKey.GetValue("Value2");
}

my C# windows application runs on system startup. but works in windows xp and not in windows 7. I have written these codes

RegistryKey rkStartUp = Registry.CurrentUser;
RegistryKey StartupPath;
StartupPath =rkStartUp.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (StartupPath.GetValue("MyApplication") == null)
{
StartupPath.SetValue("MyApplication", Application.ExecutablePath, RegistryValueKind.ExpandString);
}
else
{
StartupPath.DeleteValue("MyApplication", true);
}
First of all, be aware that registry keys are different for 32-bit and 64-bit systems. Also, in case your application is not running with admin rights, it is probably not permitted to write registry keys.

Way to write on registry location

work on C# window application.I want to write on registry.i know how to write on registry.I use bellow syntax to write on registry.
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.cre.CreateSubKey("asb");
key.SetValue("asb", "Isabella");
key.Close();
But problem is i fail to write on specified location .i want to write on bellow location
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
On this location want to add string value="abc" and ValueData="efd"
If have any query plz ask.thanks in advance.
For HKCU:
string keyName = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true);
rk.SetValue("abc", "efd");
rk.Close();
For HKLM you need to do it with administrative privileges. That requires adding a manifest to your program to invoke the UAC prompt on Vista or Win7.
Writing to HKEY_LOCAL_MACHINE requires administrative privileges. And if you're running on Windows Vista or 7, it also requires process elevation, lest you run afoul of UAC (User Account Control).
The best thing is only to write to this registry key during installation (where you will have full administrative privileges). You should only read from it once your application is installed.
Save all regular settings under HKEY_CURRENT_USER. Use the Registry.CurrentUser field to do that. Or, better yet, abandon the registry altogether and save your application's settings in a config file. Visual Studio has built-in support for this, it's very simple to do from C#. The registry is no longer the recommended way of saving application state.
RegistryKey reg = Registry.LocalMachine.
OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run", true);
// set value of "abc" to "efd"
reg.SetValue("abc", "efd", RegistryValueKind.DWord);
// get value of "abc"; return 0 if value not found
string value = (string)reg.GetValue("abc", "0");

C# 32bit app 64bit registry

I know there are lots of sites about this. I've been testing out different ideas for about 6 hours now. I'm trying to get a 32bit app to modify the 64bit registry. I need to set the permissions to HKLM\Software\Microsoft\Windows\Current Version\Installer\UserData\ If you are wondering why, it's because our software throws an error if the permissions aren't correct.
Here is what I'm trying
static bool SetRegistryPermissions(string hkLmKey, string userAccount)
{
//this will force the app to see the 64bit registry instead of being redirected
RegistryKey localMachineX64View = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey rk = localMachineX64View.OpenSubKey(hkLmKey, true);
//This redirects to the Wow6432Node in the registry
//RegistryKey rk = Registry.LocalMachine.OpenSubKey(hkLmKey, true);
The program was working fine on a test key in WoW6432Node prior to changing the key to localMachineX64. Now I get a security exception when debugging on the OpenSubKey.
Any advice is welcome and Thanks for your time.
P.S. Any suggestions for books that contain good info writing NT permissions in C# would be a bonus.
Could you create a small 64 bit application that could set the 64 bit permissions? You could then call the exe from your installer's post install event.
I'm unsure if there is a .NET approach to this, but the Windows API definitely provides a solution. You can use the RegOpenKeyEx function with KEY_WOW64_64KEY (http://msdn.microsoft.com/en-us/library/ms724878%28v=vs.85%29.aspx) included as one of the access options. This will allow your 32 bit app to access the full registry, not just the Wow6432Node sandbox.
Edit: pinvoke.net has a C# example ready to go: http://www.pinvoke.net/default.aspx/advapi32/RegOpenKeyEx.html

Categories