The Labs \ Source Viewer \ SSCLI \ System.Security.Permissions \ CodeAccessSecurityAttribute

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Security.Permissions
  16. {
  17.    
  18.     using System.Security.Util;
  19.     using System.IO;
  20.     using System.Security.Policy;
  21.     using System.Runtime.Remoting.Activation;
  22.     using System.Text;
  23.     using System.Runtime.Serialization.Formatters;
  24.     using System.Threading;
  25.     using System.Runtime.InteropServices;
  26.     using System.Runtime.Remoting;
  27.     using System.Runtime.Serialization;
  28.     using System.Security.Cryptography.X509Certificates;
  29.     using System.Runtime.Versioning;
  30.    
  31.     [Serializable()]
  32.     [System.Runtime.InteropServices.ComVisible(true)]
  33.     public enum SecurityAction
  34.     {
  35.         // Demand permission of all caller
  36.         Demand = 2,
  37.        
  38.         // Assert permission so callers don't need
  39.         Assert = 3,
  40.        
  41.         // Deny permissions so checks will fail
  42.         Deny = 4,
  43.        
  44.         // Reduce permissions so check will fail
  45.         PermitOnly = 5,
  46.        
  47.         // Demand permission of caller
  48.         LinkDemand = 6,
  49.        
  50.         // Demand permission of a subclass
  51.         InheritanceDemand = 7,
  52.        
  53.         // Request minimum permissions to run
  54.         RequestMinimum = 8,
  55.        
  56.         // Request optional additional permissions
  57.         RequestOptional = 9,
  58.        
  59.         // Refuse to be granted these permissions
  60.         RequestRefuse = 10
  61.     }
  62.    
  63.    
  64.     [Serializable(), AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  65.     [System.Runtime.InteropServices.ComVisible(true)]
  66.     public abstract class SecurityAttribute : System.Attribute
  67.     {
  68.         /// <internalonly/>
  69.         internal SecurityAction m_action;
  70.         /// <internalonly/>
  71.         internal bool m_unrestricted;
  72.        
  73.         protected SecurityAttribute(SecurityAction action)
  74.         {
  75.             m_action = action;
  76.         }
  77.        
  78.         public SecurityAction Action {
  79.             get { return m_action; }
  80.             set { m_action = value; }
  81.         }
  82.        
  83.         public bool Unrestricted {
  84.             get { return m_unrestricted; }
  85.             set { m_unrestricted = value; }
  86.         }
  87.        
  88.         public abstract IPermission CreatePermission();
  89.        
  90.         unsafe static internal IntPtr FindSecurityAttributeTypeHandle(string typeName)
  91.         {
  92.             PermissionSet.s_fullTrust.Assert();
  93.             Type t = Type.GetType(typeName, false, false);
  94.             if (t == null)
  95.                 return IntPtr.Zero;
  96.             IntPtr typeHandle = t.TypeHandle.Value;
  97.             return typeHandle;
  98.         }
  99.     }
  100.    
  101.     [Serializable(), AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  102.     [System.Runtime.InteropServices.ComVisible(true)]
  103.     public abstract class CodeAccessSecurityAttribute : SecurityAttribute
  104.     {
  105.         protected CodeAccessSecurityAttribute(SecurityAction action) : base(action)
  106.         {
  107.         }
  108.     }
  109.    
  110.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  111.     [System.Runtime.InteropServices.ComVisible(true)]
  112.     [Serializable()]
  113.     public sealed class EnvironmentPermissionAttribute : CodeAccessSecurityAttribute
  114.     {
  115.         private string m_read = null;
  116.         private string m_write = null;
  117.        
  118.         public EnvironmentPermissionAttribute(SecurityAction action) : base(action)
  119.         {
  120.         }
  121.        
  122.         public string Read {
  123.             get { return m_read; }
  124.             set { m_read = value; }
  125.         }
  126.        
  127.         public string Write {
  128.             get { return m_write; }
  129.             set { m_write = value; }
  130.         }
  131.        
  132.         public string All {
  133.             get {
  134.                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_GetMethod"));
  135.             }
  136.             set {
  137.                 m_write = value;
  138.                 m_read = value;
  139.             }
  140.         }
  141.        
  142.         public override IPermission CreatePermission()
  143.         {
  144.             if (m_unrestricted) {
  145.                 return new EnvironmentPermission(PermissionState.Unrestricted);
  146.             }
  147.             else {
  148.                 EnvironmentPermission perm = new EnvironmentPermission(PermissionState.None);
  149.                 if (m_read != null)
  150.                     perm.SetPathList(EnvironmentPermissionAccess.Read, m_read);
  151.                 if (m_write != null)
  152.                     perm.SetPathList(EnvironmentPermissionAccess.Write, m_write);
  153.                 return perm;
  154.             }
  155.         }
  156.     }
  157.    
  158.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  159.     [System.Runtime.InteropServices.ComVisible(true)]
  160.     [Serializable()]
  161.     public sealed class FileDialogPermissionAttribute : CodeAccessSecurityAttribute
  162.     {
  163.         private FileDialogPermissionAccess m_access;
  164.        
  165.         public FileDialogPermissionAttribute(SecurityAction action) : base(action)
  166.         {
  167.         }
  168.        
  169.         public bool Open {
  170.             get { return (m_access & FileDialogPermissionAccess.Open) != 0; }
  171.             set { m_access = value ? m_access | FileDialogPermissionAccess.Open : m_access & ~FileDialogPermissionAccess.Open; }
  172.         }
  173.        
  174.         public bool Save {
  175.             get { return (m_access & FileDialogPermissionAccess.Save) != 0; }
  176.             set { m_access = value ? m_access | FileDialogPermissionAccess.Save : m_access & ~FileDialogPermissionAccess.Save; }
  177.         }
  178.        
  179.         public override IPermission CreatePermission()
  180.         {
  181.             if (m_unrestricted) {
  182.                 return new FileDialogPermission(PermissionState.Unrestricted);
  183.             }
  184.             else {
  185.                 return new FileDialogPermission(m_access);
  186.             }
  187.         }
  188.     }
  189.    
  190.    
  191.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  192.     [System.Runtime.InteropServices.ComVisible(true)]
  193.     [Serializable()]
  194.     public sealed class FileIOPermissionAttribute : CodeAccessSecurityAttribute
  195.     {
  196.         private string m_read = null;
  197.         private string m_write = null;
  198.         private string m_append = null;
  199.         private string m_pathDiscovery = null;
  200.         private string m_viewAccess = null;
  201.         private string m_changeAccess = null;
  202.         [OptionalField(VersionAdded = 2)]
  203.         private FileIOPermissionAccess m_allLocalFiles = FileIOPermissionAccess.NoAccess;
  204.         [OptionalField(VersionAdded = 2)]
  205.         private FileIOPermissionAccess m_allFiles = FileIOPermissionAccess.NoAccess;
  206.        
  207.         public FileIOPermissionAttribute(SecurityAction action) : base(action)
  208.         {
  209.         }
  210.        
  211.         public string Read {
  212.             get { return m_read; }
  213.             set { m_read = value; }
  214.         }
  215.        
  216.         public string Write {
  217.             get { return m_write; }
  218.             set { m_write = value; }
  219.         }
  220.        
  221.         public string Append {
  222.             get { return m_append; }
  223.             set { m_append = value; }
  224.         }
  225.        
  226.         public string PathDiscovery {
  227.             get { return m_pathDiscovery; }
  228.             set { m_pathDiscovery = value; }
  229.         }
  230.        
  231.         public string ViewAccessControl {
  232.             get { return m_viewAccess; }
  233.             set { m_viewAccess = value; }
  234.         }
  235.        
  236.         public string ChangeAccessControl {
  237.             get { return m_changeAccess; }
  238.             set { m_changeAccess = value; }
  239.         }
  240.        
  241.         [Obsolete("Please use the ViewAndModify property instead.")]
  242.         public string All {
  243.             get {
  244.                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_GetMethod"));
  245.             }
  246.             set {
  247.                 m_read = value;
  248.                 m_write = value;
  249.                 m_append = value;
  250.                 m_pathDiscovery = value;
  251.             }
  252.         }
  253.        
  254.         // Read, Write, Append, PathDiscovery, but no ACL-related permissions
  255.         public string ViewAndModify {
  256.             get {
  257.                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_GetMethod"));
  258.             }
  259.             set {
  260.                 m_read = value;
  261.                 m_write = value;
  262.                 m_append = value;
  263.                 m_pathDiscovery = value;
  264.             }
  265.         }
  266.        
  267.         public FileIOPermissionAccess AllFiles {
  268.             get { return m_allFiles; }
  269.             set { m_allFiles = value; }
  270.         }
  271.        
  272.         public FileIOPermissionAccess AllLocalFiles {
  273.             get { return m_allLocalFiles; }
  274.             set { m_allLocalFiles = value; }
  275.         }
  276.        
  277.         public override IPermission CreatePermission()
  278.         {
  279.             if (m_unrestricted) {
  280.                 return new FileIOPermission(PermissionState.Unrestricted);
  281.             }
  282.             else {
  283.                 FileIOPermission perm = new FileIOPermission(PermissionState.None);
  284.                 if (m_read != null)
  285.                     perm.SetPathList(FileIOPermissionAccess.Read, m_read);
  286.                 if (m_write != null)
  287.                     perm.SetPathList(FileIOPermissionAccess.Write, m_write);
  288.                 if (m_append != null)
  289.                     perm.SetPathList(FileIOPermissionAccess.Append, m_append);
  290.                 if (m_pathDiscovery != null)
  291.                     perm.SetPathList(FileIOPermissionAccess.PathDiscovery, m_pathDiscovery);
  292.                
  293.                 perm.AllFiles = m_allFiles;
  294.                 perm.AllLocalFiles = m_allLocalFiles;
  295.                 return perm;
  296.             }
  297.         }
  298.     }
  299.    
  300.    
  301.     // PrincipalPermissionAttribute currently derives from
  302.     // CodeAccessSecurityAttribute, even though it's not related to code access
  303.     // security. This is because compilers are currently looking for
  304.     // CodeAccessSecurityAttribute as a direct parent class rather than
  305.     // SecurityAttribute as the root class.
  306.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
  307.     [System.Runtime.InteropServices.ComVisible(true)]
  308.     [Serializable()]
  309.     public sealed class PrincipalPermissionAttribute : CodeAccessSecurityAttribute
  310.     {
  311.         private string m_name = null;
  312.         private string m_role = null;
  313.         private bool m_authenticated = true;
  314.        
  315.         public PrincipalPermissionAttribute(SecurityAction action) : base(action)
  316.         {
  317.         }
  318.        
  319.         public string Name {
  320.             get { return m_name; }
  321.             set { m_name = value; }
  322.         }
  323.        
  324.         public string Role {
  325.             get { return m_role; }
  326.             set { m_role = value; }
  327.         }
  328.        
  329.         public bool Authenticated {
  330.             get { return m_authenticated; }
  331.             set { m_authenticated = value; }
  332.         }
  333.        
  334.        
  335.         public override IPermission CreatePermission()
  336.         {
  337.             if (m_unrestricted) {
  338.                 return new PrincipalPermission(PermissionState.Unrestricted);
  339.             }
  340.             else {
  341.                 return new PrincipalPermission(m_name, m_role, m_authenticated);
  342.             }
  343.         }
  344.     }
  345.    
  346.    
  347.    
  348.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  349.     [System.Runtime.InteropServices.ComVisible(true)]
  350.     [Serializable()]
  351.     public sealed class ReflectionPermissionAttribute : CodeAccessSecurityAttribute
  352.     {
  353.         private ReflectionPermissionFlag m_flag = ReflectionPermissionFlag.NoFlags;
  354.        
  355.         public ReflectionPermissionAttribute(SecurityAction action) : base(action)
  356.         {
  357.         }
  358.        
  359.         public ReflectionPermissionFlag Flags {
  360.             get { return m_flag; }
  361.             set { m_flag = value; }
  362.         }
  363.        
  364.         [Obsolete("This API has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")]
  365.         public bool TypeInformation {
  366.             #pragma warning disable 618
  367.             get { return (m_flag & ReflectionPermissionFlag.TypeInformation) != 0; }
  368.             set { m_flag = value ? m_flag | ReflectionPermissionFlag.TypeInformation : m_flag & ~ReflectionPermissionFlag.TypeInformation; }
  369.         }
  370.         #pragma warning restore 618
  371.        
  372.         public bool MemberAccess {
  373.             get { return (m_flag & ReflectionPermissionFlag.MemberAccess) != 0; }
  374.             set { m_flag = value ? m_flag | ReflectionPermissionFlag.MemberAccess : m_flag & ~ReflectionPermissionFlag.MemberAccess; }
  375.         }
  376.        
  377.         public bool ReflectionEmit {
  378.             get { return (m_flag & ReflectionPermissionFlag.ReflectionEmit) != 0; }
  379.             set { m_flag = value ? m_flag | ReflectionPermissionFlag.ReflectionEmit : m_flag & ~ReflectionPermissionFlag.ReflectionEmit; }
  380.         }
  381.        
  382.         public override IPermission CreatePermission()
  383.         {
  384.             if (m_unrestricted) {
  385.                 return new ReflectionPermission(PermissionState.Unrestricted);
  386.             }
  387.             else {
  388.                 return new ReflectionPermission(m_flag);
  389.             }
  390.         }
  391.     }
  392.    
  393.    
  394.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  395.     [System.Runtime.InteropServices.ComVisible(true)]
  396.     [Serializable()]
  397.     public sealed class SecurityPermissionAttribute : CodeAccessSecurityAttribute
  398.     {
  399.         private SecurityPermissionFlag m_flag = SecurityPermissionFlag.NoFlags;
  400.        
  401.         public SecurityPermissionAttribute(SecurityAction action) : base(action)
  402.         {
  403.         }
  404.        
  405.         public SecurityPermissionFlag Flags {
  406.             get { return m_flag; }
  407.             set { m_flag = value; }
  408.         }
  409.        
  410.         public bool Assertion {
  411.             get { return (m_flag & SecurityPermissionFlag.Assertion) != 0; }
  412.             set { m_flag = value ? m_flag | SecurityPermissionFlag.Assertion : m_flag & ~SecurityPermissionFlag.Assertion; }
  413.         }
  414.        
  415.         public bool UnmanagedCode {
  416.             get { return (m_flag & SecurityPermissionFlag.UnmanagedCode) != 0; }
  417.             set { m_flag = value ? m_flag | SecurityPermissionFlag.UnmanagedCode : m_flag & ~SecurityPermissionFlag.UnmanagedCode; }
  418.         }
  419.        
  420.         public bool SkipVerification {
  421.             get { return (m_flag & SecurityPermissionFlag.SkipVerification) != 0; }
  422.             set { m_flag = value ? m_flag | SecurityPermissionFlag.SkipVerification : m_flag & ~SecurityPermissionFlag.SkipVerification; }
  423.         }
  424.        
  425.         public bool Execution {
  426.             get { return (m_flag & SecurityPermissionFlag.Execution) != 0; }
  427.             set { m_flag = value ? m_flag | SecurityPermissionFlag.Execution : m_flag & ~SecurityPermissionFlag.Execution; }
  428.         }
  429.        
  430.         public bool ControlThread {
  431.             get { return (m_flag & SecurityPermissionFlag.ControlThread) != 0; }
  432.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlThread : m_flag & ~SecurityPermissionFlag.ControlThread; }
  433.         }
  434.        
  435.         public bool ControlEvidence {
  436.             get { return (m_flag & SecurityPermissionFlag.ControlEvidence) != 0; }
  437.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlEvidence : m_flag & ~SecurityPermissionFlag.ControlEvidence; }
  438.         }
  439.        
  440.         public bool ControlPolicy {
  441.             get { return (m_flag & SecurityPermissionFlag.ControlPolicy) != 0; }
  442.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlPolicy : m_flag & ~SecurityPermissionFlag.ControlPolicy; }
  443.         }
  444.        
  445.         public bool SerializationFormatter {
  446.             get { return (m_flag & SecurityPermissionFlag.SerializationFormatter) != 0; }
  447.             set { m_flag = value ? m_flag | SecurityPermissionFlag.SerializationFormatter : m_flag & ~SecurityPermissionFlag.SerializationFormatter; }
  448.         }
  449.        
  450.         public bool ControlDomainPolicy {
  451.             get { return (m_flag & SecurityPermissionFlag.ControlDomainPolicy) != 0; }
  452.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlDomainPolicy : m_flag & ~SecurityPermissionFlag.ControlDomainPolicy; }
  453.         }
  454.        
  455.         public bool ControlPrincipal {
  456.             get { return (m_flag & SecurityPermissionFlag.ControlPrincipal) != 0; }
  457.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlPrincipal : m_flag & ~SecurityPermissionFlag.ControlPrincipal; }
  458.         }
  459.        
  460.         public bool ControlAppDomain {
  461.             get { return (m_flag & SecurityPermissionFlag.ControlAppDomain) != 0; }
  462.             set { m_flag = value ? m_flag | SecurityPermissionFlag.ControlAppDomain : m_flag & ~SecurityPermissionFlag.ControlAppDomain; }
  463.         }
  464.        
  465.         public bool RemotingConfiguration {
  466.             get { return (m_flag & SecurityPermissionFlag.RemotingConfiguration) != 0; }
  467.             set { m_flag = value ? m_flag | SecurityPermissionFlag.RemotingConfiguration : m_flag & ~SecurityPermissionFlag.RemotingConfiguration; }
  468.         }
  469.        
  470.         [System.Runtime.InteropServices.ComVisible(true)]
  471.         public bool Infrastructure {
  472.             get { return (m_flag & SecurityPermissionFlag.Infrastructure) != 0; }
  473.             set { m_flag = value ? m_flag | SecurityPermissionFlag.Infrastructure : m_flag & ~SecurityPermissionFlag.Infrastructure; }
  474.         }
  475.        
  476.         public bool BindingRedirects {
  477.             get { return (m_flag & SecurityPermissionFlag.BindingRedirects) != 0; }
  478.             set { m_flag = value ? m_flag | SecurityPermissionFlag.BindingRedirects : m_flag & ~SecurityPermissionFlag.BindingRedirects; }
  479.         }
  480.        
  481.         public override IPermission CreatePermission()
  482.         {
  483.             if (m_unrestricted) {
  484.                 return new SecurityPermission(PermissionState.Unrestricted);
  485.             }
  486.             else {
  487.                 return new SecurityPermission(m_flag);
  488.             }
  489.         }
  490.     }
  491.    
  492.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  493.     [System.Runtime.InteropServices.ComVisible(true)]
  494.     [Serializable()]
  495.     public sealed class UIPermissionAttribute : CodeAccessSecurityAttribute
  496.     {
  497.         private UIPermissionWindow m_windowFlag = UIPermissionWindow.NoWindows;
  498.         private UIPermissionClipboard m_clipboardFlag = UIPermissionClipboard.NoClipboard;
  499.        
  500.         public UIPermissionAttribute(SecurityAction action) : base(action)
  501.         {
  502.         }
  503.        
  504.         public UIPermissionWindow Window {
  505.             get { return m_windowFlag; }
  506.             set { m_windowFlag = value; }
  507.         }
  508.        
  509.         public UIPermissionClipboard Clipboard {
  510.             get { return m_clipboardFlag; }
  511.             set { m_clipboardFlag = value; }
  512.         }
  513.        
  514.         public override IPermission CreatePermission()
  515.         {
  516.             if (m_unrestricted) {
  517.                 return new UIPermission(PermissionState.Unrestricted);
  518.             }
  519.             else {
  520.                 return new UIPermission(m_windowFlag, m_clipboardFlag);
  521.             }
  522.         }
  523.     }
  524.    
  525.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  526.     [System.Runtime.InteropServices.ComVisible(true)]
  527.     [Serializable()]
  528.     public sealed class ZoneIdentityPermissionAttribute : CodeAccessSecurityAttribute
  529.     {
  530.         private SecurityZone m_flag = SecurityZone.NoZone;
  531.        
  532.         public ZoneIdentityPermissionAttribute(SecurityAction action) : base(action)
  533.         {
  534.         }
  535.        
  536.         public SecurityZone Zone {
  537.             get { return m_flag; }
  538.             set { m_flag = value; }
  539.         }
  540.        
  541.         public override IPermission CreatePermission()
  542.         {
  543.             if (m_unrestricted) {
  544.                 return new ZoneIdentityPermission(PermissionState.Unrestricted);
  545.             }
  546.             else {
  547.                 return new ZoneIdentityPermission(m_flag);
  548.             }
  549.         }
  550.     }
  551.    
  552.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  553.     [System.Runtime.InteropServices.ComVisible(true)]
  554.     [Serializable()]
  555.     public sealed class StrongNameIdentityPermissionAttribute : CodeAccessSecurityAttribute
  556.     {
  557.         private string m_name = null;
  558.         private string m_version = null;
  559.         private string m_blob = null;
  560.        
  561.         public StrongNameIdentityPermissionAttribute(SecurityAction action) : base(action)
  562.         {
  563.         }
  564.        
  565.         public string Name {
  566.             get { return m_name; }
  567.             set { m_name = value; }
  568.         }
  569.        
  570.         public string Version {
  571.             get { return m_version; }
  572.             set { m_version = value; }
  573.         }
  574.        
  575.         public string PublicKey {
  576.             get { return m_blob; }
  577.             set { m_blob = value; }
  578.         }
  579.        
  580.         public override IPermission CreatePermission()
  581.         {
  582.             if (m_unrestricted) {
  583.                 return new StrongNameIdentityPermission(PermissionState.Unrestricted);
  584.             }
  585.             else {
  586.                 if (m_blob == null && m_name == null && m_version == null)
  587.                     return new StrongNameIdentityPermission(PermissionState.None);
  588.                
  589.                 if (m_blob == null)
  590.                     throw new ArgumentException(Environment.GetResourceString("ArgumentNull_Key"));
  591.                
  592.                 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob(m_blob);
  593.                
  594.                 if (m_version == null || m_version.Equals(String.Empty))
  595.                     return new StrongNameIdentityPermission(blob, m_name, null);
  596.                 else
  597.                     return new StrongNameIdentityPermission(blob, m_name, new Version(m_version));
  598.             }
  599.         }
  600.     }
  601.    
  602.    
  603.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  604.     [System.Runtime.InteropServices.ComVisible(true)]
  605.     [Serializable()]
  606.     public sealed class SiteIdentityPermissionAttribute : CodeAccessSecurityAttribute
  607.     {
  608.         private string m_site = null;
  609.        
  610.         public SiteIdentityPermissionAttribute(SecurityAction action) : base(action)
  611.         {
  612.         }
  613.        
  614.         public string Site {
  615.             get { return m_site; }
  616.             set { m_site = value; }
  617.         }
  618.        
  619.         public override IPermission CreatePermission()
  620.         {
  621.             if (m_unrestricted) {
  622.                 return new SiteIdentityPermission(PermissionState.Unrestricted);
  623.             }
  624.             else {
  625.                 if (m_site == null)
  626.                     return new SiteIdentityPermission(PermissionState.None);
  627.                
  628.                 return new SiteIdentityPermission(m_site);
  629.             }
  630.         }
  631.     }
  632.    
  633.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  634.     [System.Runtime.InteropServices.ComVisible(true)]
  635.     [Serializable()]
  636.     public sealed class UrlIdentityPermissionAttribute : CodeAccessSecurityAttribute
  637.     {
  638.         private string m_url = null;
  639.        
  640.         public UrlIdentityPermissionAttribute(SecurityAction action) : base(action)
  641.         {
  642.         }
  643.        
  644.         public string Url {
  645.             get { return m_url; }
  646.             set { m_url = value; }
  647.         }
  648.        
  649.         public override IPermission CreatePermission()
  650.         {
  651.             if (m_unrestricted) {
  652.                 return new UrlIdentityPermission(PermissionState.Unrestricted);
  653.             }
  654.             else {
  655.                 if (m_url == null)
  656.                     return new UrlIdentityPermission(PermissionState.None);
  657.                
  658.                 return new UrlIdentityPermission(m_url);
  659.             }
  660.         }
  661.     }
  662.    
  663.    
  664.     [Serializable(), AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  665.     [System.Runtime.InteropServices.ComVisible(true)]
  666.     public abstract class IsolatedStoragePermissionAttribute : CodeAccessSecurityAttribute
  667.     {
  668.         /// <internalonly/>
  669.         internal long m_userQuota;
  670.         /// <internalonly/>
  671.         internal IsolatedStorageContainment m_allowed;
  672.         protected IsolatedStoragePermissionAttribute(SecurityAction action) : base(action)
  673.         {
  674.         }
  675.        
  676.         // properties
  677.         public long UserQuota {
  678.             get { return m_userQuota; }
  679.             set { m_userQuota = value; }
  680.         }
  681.         public IsolatedStorageContainment UsageAllowed {
  682.             get { return m_allowed; }
  683.             set { m_allowed = value; }
  684.         }
  685.        
  686.     }
  687.    
  688.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  689.     [System.Runtime.InteropServices.ComVisible(true)]
  690.     [Serializable()]
  691.     public sealed class IsolatedStorageFilePermissionAttribute : IsolatedStoragePermissionAttribute
  692.     {
  693.         public IsolatedStorageFilePermissionAttribute(SecurityAction action) : base(action)
  694.         {
  695.            
  696.         }
  697.         public override IPermission CreatePermission()
  698.         {
  699.             IsolatedStorageFilePermission p;
  700.             if (m_unrestricted) {
  701.                 p = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
  702.             }
  703.             else {
  704.                 p = new IsolatedStorageFilePermission(PermissionState.None);
  705.                 p.UserQuota = m_userQuota;
  706.                 p.UsageAllowed = m_allowed;
  707.             }
  708.             return p;
  709.         }
  710.     }
  711.    
  712.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  713.     [System.Runtime.InteropServices.ComVisible(true)]
  714.     [Serializable()]
  715.     public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute
  716.     {
  717.         private string m_file;
  718.         private string m_name;
  719.         private bool m_unicode;
  720.         private string m_xml;
  721.         private string m_hex;
  722.        
  723.         public PermissionSetAttribute(SecurityAction action) : base(action)
  724.         {
  725.             m_unicode = false;
  726.         }
  727.        
  728.         public string File {
  729.             get { return m_file; }
  730.             [ResourceExposure(ResourceScope.Machine)]
  731.             [ResourceConsumption(ResourceScope.Machine)]
  732.             set { m_file = value; }
  733.         }
  734.        
  735.         public bool UnicodeEncoded {
  736.             get { return m_unicode; }
  737.             set { m_unicode = value; }
  738.         }
  739.        
  740.         public string Name {
  741.             get { return m_name; }
  742.             set { m_name = value; }
  743.         }
  744.        
  745.         public string XML {
  746.             get { return m_xml; }
  747.             set { m_xml = value; }
  748.         }
  749.        
  750.         public string Hex {
  751.             get { return m_hex; }
  752.             set { m_hex = value; }
  753.         }
  754.        
  755.         public override IPermission CreatePermission()
  756.         {
  757.             return null;
  758.         }
  759.        
  760.         private PermissionSet BruteForceParseStream(Stream stream)
  761.         {
  762.             Encoding[] encodings = new Encoding[] {Encoding.UTF8, Encoding.ASCII, Encoding.Unicode};
  763.            
  764.             StreamReader reader = null;
  765.             Exception exception = null;
  766.            
  767.             for (int i = 0; reader == null && i < encodings.Length; ++i) {
  768.                 try {
  769.                     stream.Position = 0;
  770.                     reader = new StreamReader(stream, encodings[i]);
  771.                    
  772.                     return ParsePermissionSet(new Parser(reader));
  773.                 }
  774.                 catch (Exception e1) {
  775.                     if (exception == null)
  776.                         exception = e1;
  777.                 }
  778.             }
  779.            
  780.             throw exception;
  781.         }
  782.        
  783.         private PermissionSet ParsePermissionSet(Parser parser)
  784.         {
  785.             SecurityElement e = parser.GetTopElement();
  786.             PermissionSet permSet = new PermissionSet(PermissionState.None);
  787.             permSet.FromXml(e);
  788.            
  789.             return permSet;
  790.         }
  791.        
  792.         [ResourceExposure(ResourceScope.Machine)]
  793.         [ResourceConsumption(ResourceScope.Machine)]
  794.         public PermissionSet CreatePermissionSet()
  795.         {
  796.             if (m_unrestricted)
  797.                 return new PermissionSet(PermissionState.Unrestricted);
  798.             else if (m_name != null)
  799.                 return PolicyLevel.GetBuiltInSet(m_name);
  800.             else if (m_xml != null)
  801.                 return ParsePermissionSet(new Parser(m_xml.ToCharArray()));
  802.             else if (m_hex != null)
  803.                 return BruteForceParseStream(new MemoryStream(Util.Hex.DecodeHexString(m_hex)));
  804.             else if (m_file != null)
  805.                 return BruteForceParseStream(new FileStream(m_file, FileMode.Open, FileAccess.Read));
  806.             else
  807.                 return new PermissionSet(PermissionState.None);
  808.         }
  809.     }
  810. }

Developer Fusion