views:

37

answers:

2

Is there any way, using C#, to monitor a specific file then change its contents before it is read by specific applications?

Here is the situation:

I have a Windows 2003 Server running ASP.NET with a configuration file (xml) which contains LDAP information. I want to have the LDAP password encrypted. I'm trying to devise a way to monitor that file, and whenever it is read, decrypt the LDAP password and pass that to whatever is reading it. Is there any way to tell which program is doing the read? I aldready have the encrypt/decrypt working but it is built into the ASP.NET installation; I would like to make it external. The encrypt/decrypt is RSA using key's from the key store.

+2  A: 

If you want the encrypt/decrypt external to your main application, what about creating a separate .dll or webservice that does that. Then your call in your ASP.NET application is to your webserice or .dll.

Something like (Warining: Not Compiled- you'll need to clean this up)

WebServiceInstance instance = new WebServiceInstance();
string password = instance.PerformGetPassword();

Then, your ASP.NET service is unaware of the encrypted password at all. Additionally, if you have other applications which need to access the same file, they can call the same webservice.

AllenG
+1 Webservice is the right way to create decrypted access to a file of this nature, but it doesn't resolve his issue of making *all* reads (like when notepad tries to read it) be fed a decrypted version of the file, if that is in fact a requirement..
Jimmy Hoffa
@Jimmy Hoffa- true, if _everything_ that reads it needs it decrypted, this wouldn't work. However, if _everything_ that reads it needs it decrypted, why encrypt in the first place? Set read permissions so that only authorized users/groups can read the file.
AllenG
The problem is I dont have access to/cant change the source code of all the web applications that I would like to use this for. Else this would be the way I would go.
Petey B
+1  A: 

I think this would be much better accomplished by using NTFS permissions on the file. Grant access only to certain users/groups, and ensure that any process requiring access to the secured data is running under the security context of a user that has the correct ACL permissions.

Dave Swersky
This is a good idea, if I remember correctly ASP.NET (with IIS 6) runs under Network Service. Can anyone confirm that?
Petey B
ASP.NET worker processes can run under whichever account you set, I think it is Network Service by default. Generally you want to set up security so that you don't have to worry about decrypting the data on the fly. If you're going to decrypt it for any requestor it might as well not be encrypted.
Dave Swersky