O'Reilly logo

.NET Programming: A Practical Guide Using C# by Pradeep Tapadiya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required