mscorlib(4.0.0.0) API with additions
CodeAccessPermission.cs
4 using System.Security.Util;
5 using System.Threading;
6 
7 namespace System.Security
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [SecurityPermission(SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
14  {
17  [MethodImpl(MethodImplOptions.NoInlining)]
18  [SecuritySafeCritical]
19  public static void RevertAssert()
20  {
21  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
22  SecurityRuntime.RevertAssert(ref stackMark);
23  }
24 
27  [MethodImpl(MethodImplOptions.NoInlining)]
28  [SecuritySafeCritical]
29  [Obsolete("Deny is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
30  public static void RevertDeny()
31  {
32  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
33  SecurityRuntime.RevertDeny(ref stackMark);
34  }
35 
38  [MethodImpl(MethodImplOptions.NoInlining)]
39  [SecuritySafeCritical]
40  public static void RevertPermitOnly()
41  {
42  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
43  SecurityRuntime.RevertPermitOnly(ref stackMark);
44  }
45 
48  [MethodImpl(MethodImplOptions.NoInlining)]
49  [SecuritySafeCritical]
50  public static void RevertAll()
51  {
52  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
53  SecurityRuntime.RevertAll(ref stackMark);
54  }
55 
58  [MethodImpl(MethodImplOptions.NoInlining)]
59  [SecuritySafeCritical]
60  public void Demand()
61  {
62  if (!CheckDemand(null))
63  {
64  StackCrawlMark stackMark = StackCrawlMark.LookForMyCallersCaller;
65  CodeAccessSecurityEngine.Check(this, ref stackMark);
66  }
67  }
68 
69  [MethodImpl(MethodImplOptions.NoInlining)]
70  [SecuritySafeCritical]
71  internal static void Demand(PermissionType permissionType)
72  {
73  StackCrawlMark stackMark = StackCrawlMark.LookForMyCallersCaller;
74  CodeAccessSecurityEngine.SpecialDemand(permissionType, ref stackMark);
75  }
76 
79  [MethodImpl(MethodImplOptions.NoInlining)]
80  [SecuritySafeCritical]
81  public void Assert()
82  {
83  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
84  CodeAccessSecurityEngine.Assert(this, ref stackMark);
85  }
86 
87  [MethodImpl(MethodImplOptions.NoInlining)]
88  [SecuritySafeCritical]
89  internal static void Assert(bool allPossible)
90  {
91  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
92  SecurityRuntime.AssertAllPossible(ref stackMark);
93  }
94 
97  [MethodImpl(MethodImplOptions.NoInlining)]
98  [SecuritySafeCritical]
99  [Obsolete("Deny is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
100  public void Deny()
101  {
102  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
103  CodeAccessSecurityEngine.Deny(this, ref stackMark);
104  }
105 
108  [MethodImpl(MethodImplOptions.NoInlining)]
109  [SecuritySafeCritical]
110  public void PermitOnly()
111  {
112  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
113  CodeAccessSecurityEngine.PermitOnly(this, ref stackMark);
114  }
115 
120  public virtual IPermission Union(IPermission other)
121  {
122  if (other == null)
123  {
124  return Copy();
125  }
126  throw new NotSupportedException(Environment.GetResourceString("NotSupported_SecurityPermissionUnion"));
127  }
128 
129  internal static SecurityElement CreatePermissionElement(IPermission perm, string permname)
130  {
131  SecurityElement securityElement = new SecurityElement("IPermission");
132  XMLUtil.AddClassAttribute(securityElement, perm.GetType(), permname);
133  securityElement.AddAttribute("version", "1");
134  return securityElement;
135  }
136 
137  internal static void ValidateElement(SecurityElement elem, IPermission perm)
138  {
139  if (elem == null)
140  {
141  throw new ArgumentNullException("elem");
142  }
143  if (!XMLUtil.IsPermissionElement(perm, elem))
144  {
145  throw new ArgumentException(Environment.GetResourceString("Argument_NotAPermissionElement"));
146  }
147  string text = elem.Attribute("version");
148  if (text != null && !text.Equals("1"))
149  {
150  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXMLBadVersion"));
151  }
152  }
153 
156  public abstract SecurityElement ToXml();
157 
162  public abstract void FromXml(SecurityElement elem);
163 
166  public override string ToString()
167  {
168  return ToXml().ToString();
169  }
170 
171  internal bool VerifyType(IPermission perm)
172  {
173  if (perm == null || perm.GetType() != GetType())
174  {
175  return false;
176  }
177  return true;
178  }
179 
182  public abstract IPermission Copy();
183 
188  public abstract IPermission Intersect(IPermission target);
189 
195  public abstract bool IsSubsetOf(IPermission target);
196 
201  [ComVisible(false)]
202  public override bool Equals(object obj)
203  {
204  IPermission permission = obj as IPermission;
205  if (obj != null && permission == null)
206  {
207  return false;
208  }
209  try
210  {
211  if (!IsSubsetOf(permission))
212  {
213  return false;
214  }
215  if (permission != null && !permission.IsSubsetOf(this))
216  {
217  return false;
218  }
219  }
220  catch (ArgumentException)
221  {
222  return false;
223  }
224  return true;
225  }
226 
229  [ComVisible(false)]
230  public override int GetHashCode()
231  {
232  return base.GetHashCode();
233  }
234 
235  internal bool CheckDemand(CodeAccessPermission grant)
236  {
237  return IsSubsetOf(grant);
238  }
239 
240  internal bool CheckPermitOnly(CodeAccessPermission permitted)
241  {
242  return IsSubsetOf(permitted);
243  }
244 
245  internal bool CheckDeny(CodeAccessPermission denied)
246  {
247  return Intersect(denied)?.IsSubsetOf(null) ?? true;
248  }
249 
250  internal bool CheckAssert(CodeAccessPermission asserted)
251  {
252  return IsSubsetOf(asserted);
253  }
254  }
255 }
void PermitOnly()
Prevents callers higher in the call stack from using the code that calls this method to access all re...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
static void RevertPermitOnly()
Causes any previous M:System.Security.CodeAccessPermission.PermitOnly for the current frame to be rem...
Definition: __Canon.cs:3
static void RevertDeny()
Causes any previous M:System.Security.CodeAccessPermission.Deny for the current frame to be removed a...
static void RevertAll()
Causes all previous overrides for the current frame to be removed and no longer in effect.
abstract IPermission Intersect(IPermission target)
When implemented by a derived class, creates and returns a permission that is the intersection of the...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the methods that convert permission object state to and from XML element representation.
Defines the underlying structure of all code access permissions.
override bool Equals(object obj)
Determines whether the specified T:System.Security.CodeAccessPermission object is equal to the curren...
abstract SecurityElement ToXml()
When overridden in a derived class, creates an XML encoding of the security object and its current st...
abstract bool IsSubsetOf(IPermission target)
When implemented by a derived class, determines whether the current permission is a subset of the spe...
MethodImplOptions
Defines the details of how a method is implemented.
override int GetHashCode()
Gets a hash code for the T:System.Security.CodeAccessPermission object that is suitable for use in ha...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
static void RevertAssert()
Causes any previous M:System.Security.CodeAccessPermission.Assert for the current frame to be removed...
bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
void Deny()
Prevents callers higher in the call stack from using the code that calls this method to access the re...
virtual IPermission Union(IPermission other)
When overridden in a derived class, creates a permission that is the union of the current permission ...
abstract void FromXml(SecurityElement elem)
When overridden in a derived class, reconstructs a security object with a specified state from an XML...
override string ToString()
Creates and returns a string representation of the current permission object.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
abstract IPermission Copy()
When implemented by a derived class, creates and returns an identical copy of the current permission ...
Manages the stack walk that determines whether all callers in the call stack have the required permis...
Definition: IStackWalk.cs:7
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...