I'm using .NET 3.5. Say I have a method that accesses a specific file, and a specific registry key. I want to add declarative security definitions that restrict the method so that it can only access the file and the registry key specified, and nothing else.
When I try:
[RegistryPermission(SecurityAction.PermitOnly, Read = "registry key path"]
[FileIOPermission(SecurityAction.PermitOnly, Read = "file path")]
... it lets me read the file path, but not the registry key - I get a security exception.
If I use:
[RegistryPermission(SecurityAction.Demand, Read = "registry key path"]
[FileIOPermission(SecurityAction.Demand, Read = "file path")]
... it lets me read the file and the registry key, but also lets me access other files.
Am I missing something about how these methods should be used to acheive this effect?
Edit:
The code I am using to access the registry key is:
RegistryKey rk = Registry.LocalMachine;
rk = rk.OpenSubKey("MyKey");
string registryVal = rk.GetValue("Test").ToString();
and therefore the permission declaration is:
[RegistryPermission(SecurityAction.PermitOnly, Read = @"HKEY_LOCAL_MACHINE\MyKey")]
Thanks.