http://www.developer.com/security/article.php/3483866/Code-Access-Security-with-Microsoft-NET-Framework.htm
This installment of .NET Nuts & Bolts is part one of a two-part series exploring code access security and how it is controlled by the Microsoft .NET Framework. The Microsoft .NET Framework includes a number of security features that assist you in developing secure applications. The security system, which is a fundamental part of the common language runtime (CLR), controls execution of .NET code. It includes handy features such as the following: In addition, the .NET Framework provides the concept of evidence-based security. Evidence-based security works on top of the security provided by the operating system. For example, it works on top of Win32 security but is not a replacement for Win32 security. While Win32 is based on the user, the evidence-based security is based on the assembly. It gathers and presents information (or evidence) about the assembly to the security system, which then determines whether or not to allow the code to execute. For example, if code tries to read a file during execution, the security system verifies that the assembly has the required permissions and either grants access or throws a SecurityException. Evidence about an assembly can be controlled and influenced through things like strongly named assemblies, Authenticode signatures, or other custom information. Evidence is mapped to permissions through security policies, which rely on permission sets, code groups, and policy levels (enterprise, machine, and user settings) to achieve the mapping. Policies can be deployed throughout your organization through the Active Directory, but this discussion doesn't get into the specifics of that. Rather than explaining all of the concepts up front, this tutorial just dives right into an example and explains the concepts along the way. The example is a sample Windows Forms application that demonstrates the use of permission sets and code groups. The example code will try to read the contents of a file and display a message indicating success or failure. A strong name for the assembly will serve as the evidence to assign permissions. Figure 1. Create a New Permission Set Figure 2. Available Security Permissions Figure 3. Available User Interface Permissions Figure 4. Create a Code Group Figure 5. Choose the Strong Name as the Condition Type Figure 6. Ensure That Other Policies Won't Interfere You have established a code group and the evidence that will result in membership in the code group. Now, demonstrate the use of the code group by adding some code to your project and trying to execute it. Figure 7. The Evidence Presented Put You into a Restricted Group Figure 8. Information on the Generated SecurityException Figure 9. Permission Settings to Grant Access to Read a File This lengthy example demonstrated how you could use the security policy to allow or deny actions to assemblies. The second part of this two-part series will explore the System.Security.Permissions namespace. It looks at imperative and declarative security checks and overriding security checks with the Assert() method. Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the second year in a row. You can reach Mark at mstrawmyer@crowechizek.com.
Code Access Security with Microsoft .NET Framework
February 17, 2005
Evidence-Based Security
Code Group Example
Step 1: Create the Strong Name
Step 2: Create a Permission Set



Step 3: Create a Code Group



Step 4: Try to Read a File
try{ Form1.SplitFileByLine(@"c:\helloworld.txt"); MessageBox.Show("Able to read", "Read OK");}catch( SecurityException securityEx ){ MessageBox.Show( securityEx.Message, "Security Exception" );}catch( IOException fileException ){ MessageBox.Show( fileException.Message, "IO Exception" );}/// <summary>/// Read the contents of the file and return an array of lines/// </summary>/// <param name="FileName">Location of the file.</param>public static string[] SplitFileByLine(string FileName){ StreamReader fileReader = null; try { // Read the contents of the entire file fileReader = new StreamReader(FileName); System.Text.StringBuilder fileContents = new System.Text.StringBuilder(); char[] buffer = new char[32768]; while( fileReader.ReadBlock(buffer, 0, buffer.Length) -> 0 ) { fileContents.Append(buffer); buffer = new char[32768]; } // Separate the contents of the file into lines fileContents = fileContents.Replace("\r\n", "\n"); return fileContents.ToString().Split('\n'); } finally { if( fileReader != null ) { fileReader.Close(); } }}

Step 5: Grant Access to Read a File

Stay Tuned for Part 2
About the Author