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");
}
Related
var rawData = Convert.FromBase64String(_signingKey);
var cng = CngKey.Import(rawData, CngKeyBlobFormat.Pkcs8PrivateBlob);
I use this code to extract key, from embedded base64 string.
It works fine when I test it locally but when I publish on azure I get following exception:
WindowsCryptographicException: The system cannot find the file specified
(once again I'm not reading from any file)
I need this to communicate with apple apns for push notifications, is there any workaround?
And this happens only on free service plan, if I switch to basic plan it's working.
I ran into the same error after publishing an existing application to Azure. In my case the problem was solved after I set WEBSITE_LOAD_USER_PROFILE = 1 in App Services / App Name / Application Settings.
Setting WEBSITE_LOAD_USER_PROFILE to equal 1 in the Azure App Service configuration definitely got my remote iOS notifications working. Using dotAPNS for C# .NET I also needed to omit apns.UseSandbox().
It seems that it causes by there is no certificate attached in your Azure Mobile App. If it is that case, we need to upload the "Development" or "Distribution" SSL certificate to the WebApp. More info about how to send push notifications to iOS App, please refer to the azure document.
I've had a similar error trying to construct a X509Certificate2 from a byte array - worked fine locally but once I deploy to Azure Web App, I got the same and VERY misleading file not found exception.
The real issue turned out to be that there was no user store associated with the web service account. You can also get a similar error if there are permission-related errors with accessing the certificate store on Windows.
In any case - In my scenario I fixed the problem by using MachineKeySet:
new X509Certificate2(certRawBytes, default(string), X509KeyStorageFlags.MachineKeySet);
So, in your scenario, try something like:
var keyParams = new CngKeyCreationParameters
{
KeyCreationOptions = CngKeyCreationOptions.MachineKey,
};
CngKey.Create(CngAlgorithm.Rsa, keyName, keyParams);
Note: You may have to set a few parameters to get the above working. The Import method doesn't seem to support MachineKey - but you should be able to achieve similar outcome by using the Create method.
To add to #strohmsn's answer, you can also set the App Service settings with this value directly within Visual Studio on the Publish page for web apps: Right click on web app and select Publish, then select App Service Settings, and you can add setting properties there: WEBSITE_LOAD_USER_PROFILE = 1 in this case. See screenshot:
For making it works, I needed TWO things in AzureWebApp..
So my code is :
//I load the PrivateKey here
ReadedByte = System.IO.File.ReadAllBytes(strPathPrivateKey);
//create the RSA thing
RSA rsa = System.Security.Cryptography.RSA.Create();
//import the key. It crashed HERE with the 'System cannot find file specified'
rsa.ImportPkcs8PrivateKey(source: ReadedByte,bytesRead: out int _);
It works perfectly locally. But, to make it WORK on Azure Web App, I had to have those TWO requirements :
1 - the WEBSITE_LOAD_USER_PROFILE = 1 spoken in the discussion above and below
2 - The App Service Plan must include "Custom domains / SSL" !
...so No 'F1 Share Infrastructure' nor 'D1 Share Infrastructure'. The lowest Service plan that worked for me was 'B1 - 100 Total Acu'.
Maybe I have something wrong somewhere else in my code, or my 'RSA' choice is bad..anyway...
It now works!
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
I have 2 server side (dummy) programs which creates a TCP server (TCPListener) and then tries to authenticate the server with X509 certificates (BeginAuthenticateAsServer).
The client program is ready.
The difference between the two server side program is that one of them is a simple console application while the other is a Windows service.
For some reason client can connect to the console application but not to the service. Design is the same in both program.
I'm using the following line to describe the certificate I will use:
serverCertificate = new X509Certificate( "C:\\Users\\Tom\\workspace\\ServerSSL.cer", "12345678" );
I think something is fishy about privilages with the service program but I could not figured out in the last couple of days. Of course, I have the The server mode SSL must use a certificate with the associated private key. error. When I tried to search for answer as help, I got results in topics of IIS / webservices but I'm using a simple Windows 7 Pro.
May I ask your help?
The file ServerSSL.cer most likely contains only certificate, not the private key. That's what the error message tells you.
Try to find pfx or p12 file. Or if you have separate file that contains private key (i.e. .key) you need to make a pfx (p12) file from both of them (.key and .cer). You could use openssl or xca to do that.
I'm trying to access a network share \\\\ip\c$\imp\ from a web app.
AppPool is using a domain service account for identity.
Domain service account has full access to \ip\c$\imp\
AppPool is running in integrated mode - framework 4.0.
But I still get System.UnauthorizedAccessException. Any idea why?
FileAttributes attributes = File.GetAttributes("\\ip\c$\imp\testfile.txt");
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
Console.WriteLine("read-only file");
}
else
{
Console.WriteLine("not read-only file");
}
It looks like you've forgotten to escape the backslashes in the path.
A general purpose tool for tracking down problems such as this one is the Windows Sysinternals Process Monitor ProcMon. Set up a filter for your application (or for part of the path name, such as Path contains file.txt) and see what turns up. In the case of incorrectly escaped characters, you'll see a mangled pathname.
This is failing if ip in \ip\c$\imp\testfile.txt is the same ip of the webserver that the app is running on, otherwise it works just fine.
I am making a Service Program where it will change the start up type of certain service.
My Current code does not even let me open the registry for writing, even when I "Run As Admin".
Code:
ServiceKey = Registry.LocalMachine;
ServiceKey = ServiceKey.OpenSubKey(#"SYSTEM\CurrentControlSet\services\" + ServiceName, true);
ServiceKey.SetValue("Start", 2, RegistryValueKind.DWord);
However, I get this error: Requested Registry Access is not allowed.
Anyone know a solution ?
(PS. I know there are other way's I could be doing this, but it's bother me how It's not letting me Access the registry.) My program is also running as Any CPU.
The way to change a service's configuration is not to whack the registry. You use the service control manager. MSDN even has a sample program that changes a service's start type. I found this page by going to About Services, then clicking Service Configuration Programs, then Service Configuration.
Found the problem, on some registry, if you check the "Permission", you will notice that not even an admin has the permission to change the registry. You can change the permission your self but on my case, I will find a different approach to editing the service start up.