I have written a C# 2.0 application which writes to (creates subkeys and data
values) in the registry under HLM\SOFTWARE. This application works fine when
logged in as admin. But it fails when logged into another (non-admin) account.
Of course, this is what I expect.
However, I am looking for a way, upon program start, for the system to look at
my program, see what permissions it requires, and terminate (with some warning)
if the current user context does not fulfill one or more security requirement.
It seems like this should be easy to do. But no matter what, the program runs
until a call is made to modify the registry and then throws a security
exception. I really don't want my program to run at all unless it will be able
to succeed in diddling the registry.
I have tried the following in the main executable's assemblyinfo file (note,
however, that the method which modifies the registry is in another assembly):
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
All = "HKEY_LOCAL_MACHINE\\SOFTWARE")]
The above is almost verbatim from the docs for RegistryKey.CreateSubKey().
Since this is an attribute of the main program's assembly, I'd think the
runtime should see this, check it against the current context, and see that the
non-admin user will NOT be granted permission. With this knowledge, I'd think
the runtime would not permit the executable to run.
But I guess that something could be done dynamically which changes things so it
will succeed. So maybe the runtime cannot do this automatically and I have to
do something myself.