Source code for RequestRefuse / RequestOptional

Source code for RequestRefuse / RequestOptional

  • Comments 1

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("\r\n\r\n----------\r\n");
      TestRefuse();
    }

    static void TestOptional()
    {
      OptionalTest t = new OptionalTest();
      Console.WriteLine("Optional tests:\r\n");

      Console.WriteLine("\r\nBasic execution:");
      try  { Console.WriteLine(t.ReturnAString()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nMessageBox:");
      try  { t.ShowMessageBox(); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nEnvironment:");
      try  { Console.WriteLine(t.GetUsername()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nFile IO:");
      try  { Console.WriteLine(t.ReadFile()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
    }

    static void TestRefuse()
    {
      RefuseTest t = new RefuseTest();
      Console.WriteLine("Refuse tests:\r\n");

      Console.WriteLine("\r\nBasic execution:");
      try  { Console.WriteLine(t.ReturnAString()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nMessageBox:");
      try  { t.ShowMessageBox(); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nEnvironment:");
      try  { Console.WriteLine(t.GetUsername()); }
      catch(Exception ex) { Console.WriteLine(ex.Message); }
     
      Console.WriteLine("\r\nFile 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!

 

Page 1 of 1 (1 items)