Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Requested registry access is not allowed.
Welcome to the fun, fun world of code access security... Your application is probably not running on your local machine, and thus the framework denies access to the registry and event logs (amongst other things).
I'm going to walk you through creating your own code group, assigning your application to it and specifying permissions.
You'll have to strongly name your assembly for this to work based on this example, however you may use the assembly URL - follows much of the same procedure.
Open up Start > Control Panel > Administrative Tools > ".NET Configuration 1.1".
Expand "Runtime Security Policy", "Machine" and "Code Groups".
Right click on "All_Code" and select "New".
Enter a name for your code group, e.g. "MyApplication" and click "Next".
In the dropdown list, select "Strong Name".
Click the "Import" button, browse to and select your application, and click "Next".
For the purposes of this example, we'll use "Full Trust", however one may create a new permission set with the minimum requirements for your application (e.g. SQL client & event log access, etc)
Select "Full Trust" from the dropdown list, and click "Next", then click "Finish".
And to verify, open up the Visual Studio Command prompt, and navigate to your binary location (e.g. C:\My Projects\EventLogAccess_CAS\bin\debug\EventLogAccess_CAS.exe). Type in the following and hit <Enter>:
caspol -rsg EventLogAccess_CAS.exe
The screenshot "caspol_Verify.png" shows the output based on my configuration.
I'll include the zipped C# project in the next post.
Last edited by axion_sa; Jan 28th, 2006 at 01:27 PM.
Another cause of what you are experiencing imay be that you may be running a web application under partial trust (ie. the trust level in web.config is not set to "Full").
This is good, as it's best to not run your web apps under full trust. However, the event log and registry access will not run unless you create an intermediate 'Sandbox' assembly, asserting full trust.
Here is a link that describes how to create a Sandbox assembly: