Source code for RequestRefuse / RequestOptional


Here is the source code for the blog on using RequestRefuse and RequestOptional.

Refuse.cs:


using System;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;

// Let us show MessageBox windows
[assembly:UIPermission(SecurityAction.RequestMinimum, Window=UIPermissionWindow.SafeSubWindows)]

// Refuse access to the Environment
[assembly:EnvironmentPermission(SecurityAction.RequestRefuse, Unrestricted=true)]

namespace Refuse
{
  public class RefuseTest
  {

    public string ReturnAString()
    {
      return “Hello from ” + this.GetType().Name;
    }

    public void ShowMessageBox()
    {
      MessageBox.Show(“Hello from ” + this.GetType().Name);
    }

    public string GetUsername()
    {
      return Environment.UserName;
    }

    public string ReadFile()
    {
      StreamReader sr = File.OpenText(“c:\temp\test.txt”);
      string result = sr.ReadToEnd();
      sr.Close();
      return result;
    }
  }
}


Optional.cs:


using System;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;

// Let us show MessageBox windows
[assembly:UIPermission(SecurityAction.RequestMinimum, Window=UIPermissionWindow.SafeSubWindows)]

// Nothing else is required
[assembly:PermissionSet(SecurityAction.RequestOptional, Name=”Execution”)]

namespace Optional
{
 public class OptionalTest
 {
    public string ReturnAString()
    {
      return “Hello from ” + this.GetType().Name;
    }

    public void ShowMessageBox()
    {
      MessageBox.Show(“Hello from ” + this.GetType().Name);
    }

    public string GetUsername()
    {
      return Environment.UserName;
    }
 
    public string ReadFile()
    {
      StreamReader sr = File.OpenText(“c:\temp\test.txt”);
      string result = sr.ReadToEnd();
      sr.Close();
      return result;
    }
  }
}


Main.cs:


using System;
using Optional;
using Refuse;

namespace RequestDemoForBlog
{
 class Class1
 {
    [STAThread]
    static void Main(string[] args)
    {
      TestOptional();
      Console.WriteLine(“rnrn———-rn”);
      TestRefuse();
    }

    static void TestOptional()
    {
      OptionalTest t = new OptionalTest();
      Console.WriteLine(“Optional tests:rn”);

      Console.WriteLine(“rnBasic execution:”);
      try  { Console.WriteLine(t.ReturnAString()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnMessageBox:”);
      try  { t.ShowMessageBox(); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnEnvironment:”);
      try  { Console.WriteLine(t.GetUsername()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnFile IO:”);
      try  { Console.WriteLine(t.ReadFile()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
    }

    static void TestRefuse()
    {
      RefuseTest t = new RefuseTest();
      Console.WriteLine(“Refuse tests:rn”);

      Console.WriteLine(“rnBasic execution:”);
      try  { Console.WriteLine(t.ReturnAString()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnMessageBox:”);
      try  { t.ShowMessageBox(); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnEnvironment:”);
      try  { Console.WriteLine(t.GetUsername()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine(“rnFile IO:”);
      try  { Console.WriteLine(t.ReadFile()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
    }
  }
}


Build.bat:


@echo off
csc /nologo /t:library Optional.cs
csc /nologo /t:library Refuse.cs
csc /nologo /r:refuse.dll,optional.dll main.cs
echo.
echo Done!