Code Access Security

To understand CAS, let's develop a simple program that accesses the local file system. Here is the relevant code excerpt:

// Project Evidence/Foo

[assembly: AssemblyVersionAttribute("1.2.3.4")]
[assembly: AssemblyKeyFileAttribute(@".\Bin\MyKey.snk")]

public class Foo {
     public void Store(String s){
       FileStream f = new FileStream("Foo.log",
         FileMode.Append, FileAccess.Write);
       StreamWriter w = new StreamWriter(f);
       w.WriteLine(s);
       w.Close();
       f.Close();
     }
}

Method Store simply opens a local file Foo.log, appends the specified string to the file, and closes the file.

We will build this code as a library application, Foo.dll. Note that the assembly is signed with a strong name. This is needed because we will download the assembly ...

Get .NET Programming: A Practical Guide Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.