I've inherited development responsibilities for a set of .NET projects which include Windows services, an ASP.NET website and standard executables which are launched via task manager.
All of these components connect to a MySQL database. Currently, the encrypted connection string is located in the individual app.config/web.config files. I think it's kind of a pain to have to update the connection string in multiple places, so I had the idea to put the connection string in a text file which would be accessed by all the individual components.
Before I move full-steam ahead on that, I wanted to ask if I might be introducing a bug since there could potentially be multiple processes trying to access this text file at the same time. I've written a universal method for reading the text file, and I was careful to ensure that the file is being opened in "Read" mode:
public string AESDecryptFromFile(string path, string password, string IV)
{
if (!File.Exists(path))
throw new Exception("File not found: " + path);
string cryptogram;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
cryptogram = sr.ReadToEnd().Trim(new char[] { '\r', '\n', ' ' });
}
}
return AESDecrypt(cryptogram, password, IV);
}
I don't have any reason to believe that the processes would lock the file if I implement it this way, but I thought I'd ask.
Thanks in advance!
By default with the (string path, FileMode mode, FileAccess access)
constructor of FileStream
will get the following configuration as well:
Because you don't want the process to lock the file, this will be perfect for you as you'll require Read only (See https://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.110).aspx)
However if you want to be explicit you can use the following constructor:
FileStream s = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);