Quantcast
Channel: Glen's old blog
Viewing all articles
Browse latest Browse all 23

Code Access Security - Demo Code

$
0
0

This class handles opening a file and returning the contents.

 

using System;
using System.Security.Permissions;
using System.Security;
using System.IO;
[assembly: FileIOPermission(SecurityAction.RequestMinimum, Read = @"c:\temp.txt")]
namespace FileIOClass
{

 public class SecureIO
 {
  private string fname = @"c:\temp.txt";
  public SecureIO()
  {

  }

  public string OpenFile()
  {
   StreamReader sr = new StreamReader(fname);
   return sr.ReadToEnd();
  }
  public string OpenFileWithAssert()
  {
   FileIOPermission filePerm = new FileIOPermission(
    FileIOPermissionAccess.AllAccess, fname);
   filePerm.Assert();
   StreamReader sr = new StreamReader(fname);
   string s = sr.ReadToEnd();
   CodeAccessPermission.RevertAssert();
   return s;
  }
  public string OpenFileWithDemandAndAssert()
  {
   RegistryPermission regPerm = new RegistryPermission(PermissionState.Unrestricted);
   regPerm.Demand();
   FileIOPermission filePerm = new FileIOPermission(
    FileIOPermissionAccess.AllAccess, fname);
   filePerm.Assert();
   StreamReader sr = new StreamReader(fname);
   string s = sr.ReadToEnd();
   CodeAccessPermission.RevertAssert();
   return s;
  }
 }
}

 

 

 


Viewing all articles
Browse latest Browse all 23

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>