mscorlib(4.0.0.0) API with additions
AspNetHostingPermission.cs
1 using System.Security;
3 
4 namespace System.Web
5 {
7  [Serializable]
9  {
10  private AspNetHostingPermissionLevel _level;
11 
15  {
16  get
17  {
18  return _level;
19  }
20  set
21  {
22  VerifyAspNetHostingPermissionLevel(value, "Level");
23  _level = value;
24  }
25  }
26 
27  internal static void VerifyAspNetHostingPermissionLevel(AspNetHostingPermissionLevel level, string arg)
28  {
29  switch (level)
30  {
32  case AspNetHostingPermissionLevel.Minimal:
34  case AspNetHostingPermissionLevel.Medium:
36  case AspNetHostingPermissionLevel.Unrestricted:
37  return;
38  }
39  throw new ArgumentException(arg);
40  }
41 
47  {
48  switch (state)
49  {
50  case PermissionState.Unrestricted:
51  _level = AspNetHostingPermissionLevel.Unrestricted;
52  break;
53  case PermissionState.None:
54  _level = AspNetHostingPermissionLevel.None;
55  break;
56  default:
57  throw new ArgumentException(SR.GetString("InvalidArgument", state.ToString(), "state"));
58  }
59  }
60 
64  {
65  VerifyAspNetHostingPermissionLevel(level, "level");
66  _level = level;
67  }
68 
72  public bool IsUnrestricted()
73  {
74  return _level == AspNetHostingPermissionLevel.Unrestricted;
75  }
76 
79  public override IPermission Copy()
80  {
81  return new AspNetHostingPermission(_level);
82  }
83 
89  public override IPermission Union(IPermission target)
90  {
91  if (target == null)
92  {
93  return Copy();
94  }
95  if (target.GetType() != typeof(AspNetHostingPermission))
96  {
97  throw new ArgumentException(SR.GetString("InvalidArgument", (target == null) ? "null" : target.ToString(), "target"));
98  }
99  AspNetHostingPermission aspNetHostingPermission = (AspNetHostingPermission)target;
100  if (Level >= aspNetHostingPermission.Level)
101  {
102  return new AspNetHostingPermission(Level);
103  }
104  return new AspNetHostingPermission(aspNetHostingPermission.Level);
105  }
106 
112  public override IPermission Intersect(IPermission target)
113  {
114  if (target == null)
115  {
116  return null;
117  }
118  if (target.GetType() != typeof(AspNetHostingPermission))
119  {
120  throw new ArgumentException(SR.GetString("InvalidArgument", (target == null) ? "null" : target.ToString(), "target"));
121  }
122  AspNetHostingPermission aspNetHostingPermission = (AspNetHostingPermission)target;
123  if (Level <= aspNetHostingPermission.Level)
124  {
125  return new AspNetHostingPermission(Level);
126  }
127  return new AspNetHostingPermission(aspNetHostingPermission.Level);
128  }
129 
136  public override bool IsSubsetOf(IPermission target)
137  {
138  if (target == null)
139  {
140  return _level == AspNetHostingPermissionLevel.None;
141  }
142  if (target.GetType() != typeof(AspNetHostingPermission))
143  {
144  throw new ArgumentException(SR.GetString("InvalidArgument", (target == null) ? "null" : target.ToString(), "target"));
145  }
146  AspNetHostingPermission aspNetHostingPermission = (AspNetHostingPermission)target;
147  return Level <= aspNetHostingPermission.Level;
148  }
149 
156  public override void FromXml(SecurityElement securityElement)
157  {
158  if (securityElement == null)
159  {
160  throw new ArgumentNullException(SR.GetString("AspNetHostingPermissionBadXml", "securityElement"));
161  }
162  if (!securityElement.Tag.Equals("IPermission"))
163  {
164  throw new ArgumentException(SR.GetString("AspNetHostingPermissionBadXml", "securityElement"));
165  }
166  string text = securityElement.Attribute("class");
167  if (text == null)
168  {
169  throw new ArgumentException(SR.GetString("AspNetHostingPermissionBadXml", "securityElement"));
170  }
171  if (text.IndexOf(GetType().FullName, StringComparison.Ordinal) < 0)
172  {
173  throw new ArgumentException(SR.GetString("AspNetHostingPermissionBadXml", "securityElement"));
174  }
175  string strA = securityElement.Attribute("version");
176  if (string.Compare(strA, "1", StringComparison.OrdinalIgnoreCase) != 0)
177  {
178  throw new ArgumentException(SR.GetString("AspNetHostingPermissionBadXml", "version"));
179  }
180  string text2 = securityElement.Attribute("Level");
181  if (text2 == null)
182  {
183  _level = AspNetHostingPermissionLevel.None;
184  }
185  else
186  {
188  }
189  }
190 
193  public override SecurityElement ToXml()
194  {
195  SecurityElement securityElement = new SecurityElement("IPermission");
196  securityElement.AddAttribute("class", GetType().FullName + ", " + GetType().Module.Assembly.FullName.Replace('"', '\''));
197  securityElement.AddAttribute("version", "1");
198  securityElement.AddAttribute("Level", Enum.GetName(typeof(AspNetHostingPermissionLevel), _level));
199  if (IsUnrestricted())
200  {
201  securityElement.AddAttribute("Unrestricted", "true");
202  }
203  return securityElement;
204  }
205  }
206 }
static string GetName(Type enumType, object value)
Retrieves the name of the constant in the specified enumeration that has the specified value.
Definition: Enum.cs:482
Allows a permission to expose an unrestricted state.
AspNetHostingPermissionLevel
Specifies the trust level that is granted to an ASP.NET Web application.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
AspNetHostingPermission(AspNetHostingPermissionLevel level)
Initializes a new instance of the T:System.Web.AspNetHostingPermission class with the specified permi...
override SecurityElement ToXml()
Creates an XML encoding of the permission object and its current state.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
override IPermission Intersect(IPermission target)
When implemented by a derived class, creates and returns a permission that is the intersection of the...
Definition: __Canon.cs:3
string Tag
Gets or sets the tag name of an XML element.
AspNetHostingPermission(PermissionState state)
Initializes a new instance of the T:System.Web.AspNetHostingPermission class with the specified T:Sys...
bool IsUnrestricted()
Returns a value indicating whether unrestricted access to the resource that is protected by the curre...
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
Provides the base class for enumerations.
Definition: Enum.cs:14
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the underlying structure of all code access permissions.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
AspNetHostingPermissionLevel Level
Gets or sets the current hosting permission level for an ASP.NET application.
The exception that is thrown when one of the arguments provided to a method is not valid.
Attribute can be applied to a module.
Controls access permissions in ASP.NET hosted environments. This class cannot be inherited.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
override bool IsSubsetOf(IPermission target)
Returns a value indicating whether the current permission is a subset of the specified permission.
override void FromXml(SecurityElement securityElement)
Reconstructs a permission object with a specified state from an XML encoding.
override IPermission Copy()
When implemented by a derived class, creates and returns an identical copy of the current permission ...