CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Transparent Security Model

432 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Security;
   6:  using System.Security.Permissions;
   7:   
   8:  [assembly: AllowPartiallyTrustedCallers]
   9:  [assembly: SecurityRules(SecurityRuleSet.Level2)]
  10:  namespace TransparentSecurity
  11:  {
  12:      class Program
  13:      {
  14:          static void Main(string[] args)
  15:          {
  16:              var criticalFoo = new CriticalFoo();
  17:              try
  18:              {
  19:                  criticalFoo.DoDangerousOperation(AppDomain.CurrentDomain.BaseDirectory);
  20:              }
  21:              catch (Exception ex)
  22:              {
  23:                  Console.ForegroundColor = ConsoleColor.Red;
  24:                  Console.WriteLine(ex.Message);
  25:              }
  26:   
  27:              try
  28:              {
  29:                  Console.ResetColor();
  30:                  criticalFoo.DoSafeOperation(AppDomain.CurrentDomain.BaseDirectory);
  31:              }
  32:              catch (Exception ex)
  33:              {
  34:                  Console.ForegroundColor = ConsoleColor.Red;
  35:                  Console.WriteLine(ex.Message);
  36:              }
  37:   
  38:              Console.ReadLine();
  39:         }
  40:      }
  41:   
  42:      public class CriticalFoo
  43:      {
  44:          [SecuritySafeCritical]
  45:          public void DoSafeOperation (string path)
  46:          {
  47:              var prm = new FileIOPermission(FileIOPermissionAccess.AllAccess, path);
  48:              prm.Demand();
  49:              DoDangerousOperation(path);
  50:          }
  51:   
  52:          [SecurityCritical]
  53:          public void DoDangerousOperation (string path)
  54:          {
  55:              Console.ForegroundColor = ConsoleColor.Magenta;
  56:              Console.WriteLine(path);
  57:          }
  58:      }
  59:  }
by bnaya
  February 05, 2010 @ 3:35am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate