mscorlib(4.0.0.0) API with additions
UrlIdentityPermission.cs
1 using System.Collections;
5 using System.Security.Util;
6 
8 {
10  [Serializable]
11  [ComVisible(true)]
12  public sealed class UrlIdentityPermission : CodeAccessPermission, IBuiltInPermission
13  {
14  [OptionalField(VersionAdded = 2)]
15  private bool m_unrestricted;
16 
17  [OptionalField(VersionAdded = 2)]
18  private URLString[] m_urls;
19 
20  [OptionalField(VersionAdded = 2)]
21  private string m_serializedPermission;
22 
23  private URLString m_url;
24 
28  public string Url
29  {
30  get
31  {
32  if (m_urls == null)
33  {
34  return "";
35  }
36  if (m_urls.Length == 1)
37  {
38  return m_urls[0].ToString();
39  }
40  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
41  }
42  set
43  {
44  m_unrestricted = false;
45  if (value == null || value.Length == 0)
46  {
47  m_urls = null;
48  return;
49  }
50  m_urls = new URLString[1];
51  m_urls[0] = new URLString(value);
52  }
53  }
54 
55  [OnDeserialized]
56  private void OnDeserialized(StreamingContext ctx)
57  {
58  if (m_serializedPermission != null)
59  {
60  FromXml(SecurityElement.FromString(m_serializedPermission));
61  m_serializedPermission = null;
62  }
63  else if (m_url != null)
64  {
65  m_unrestricted = false;
66  m_urls = new URLString[1];
67  m_urls[0] = m_url;
68  m_url = null;
69  }
70  }
71 
72  [OnSerializing]
73  private void OnSerializing(StreamingContext ctx)
74  {
75  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
76  {
77  m_serializedPermission = ToXml().ToString();
78  if (m_urls != null && m_urls.Length == 1)
79  {
80  m_url = m_urls[0];
81  }
82  }
83  }
84 
85  [OnSerialized]
86  private void OnSerialized(StreamingContext ctx)
87  {
88  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
89  {
90  m_serializedPermission = null;
91  m_url = null;
92  }
93  }
94 
99  {
100  switch (state)
101  {
102  case PermissionState.Unrestricted:
103  m_unrestricted = true;
104  break;
105  case PermissionState.None:
106  m_unrestricted = false;
107  break;
108  default:
109  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
110  }
111  }
112 
118  public UrlIdentityPermission(string site)
119  {
120  if (site == null)
121  {
122  throw new ArgumentNullException("site");
123  }
124  Url = site;
125  }
126 
127  internal UrlIdentityPermission(URLString site)
128  {
129  m_unrestricted = false;
130  m_urls = new URLString[1];
131  m_urls[0] = site;
132  }
133 
134  internal void AppendOrigin(ArrayList originList)
135  {
136  if (m_urls == null)
137  {
138  originList.Add("");
139  return;
140  }
141  for (int i = 0; i < m_urls.Length; i++)
142  {
143  originList.Add(m_urls[i].ToString());
144  }
145  }
146 
149  public override IPermission Copy()
150  {
151  UrlIdentityPermission urlIdentityPermission = new UrlIdentityPermission(PermissionState.None);
152  urlIdentityPermission.m_unrestricted = m_unrestricted;
153  if (m_urls != null)
154  {
155  urlIdentityPermission.m_urls = new URLString[m_urls.Length];
156  for (int i = 0; i < m_urls.Length; i++)
157  {
158  urlIdentityPermission.m_urls[i] = (URLString)m_urls[i].Copy();
159  }
160  }
161  return urlIdentityPermission;
162  }
163 
169  public override bool IsSubsetOf(IPermission target)
170  {
171  if (target == null)
172  {
173  if (m_unrestricted)
174  {
175  return false;
176  }
177  if (m_urls == null)
178  {
179  return true;
180  }
181  if (m_urls.Length == 0)
182  {
183  return true;
184  }
185  return false;
186  }
187  UrlIdentityPermission urlIdentityPermission = target as UrlIdentityPermission;
188  if (urlIdentityPermission == null)
189  {
190  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
191  }
192  if (urlIdentityPermission.m_unrestricted)
193  {
194  return true;
195  }
196  if (m_unrestricted)
197  {
198  return false;
199  }
200  if (m_urls != null)
201  {
202  URLString[] urls = m_urls;
203  foreach (URLString uRLString in urls)
204  {
205  bool flag = false;
206  if (urlIdentityPermission.m_urls != null)
207  {
208  URLString[] urls2 = urlIdentityPermission.m_urls;
209  foreach (URLString operand in urls2)
210  {
211  if (uRLString.IsSubsetOf(operand))
212  {
213  flag = true;
214  break;
215  }
216  }
217  }
218  if (!flag)
219  {
220  return false;
221  }
222  }
223  }
224  return true;
225  }
226 
231  public override IPermission Intersect(IPermission target)
232  {
233  if (target == null)
234  {
235  return null;
236  }
237  UrlIdentityPermission urlIdentityPermission = target as UrlIdentityPermission;
238  if (urlIdentityPermission == null)
239  {
240  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
241  }
242  if (m_unrestricted && urlIdentityPermission.m_unrestricted)
243  {
244  UrlIdentityPermission urlIdentityPermission2 = new UrlIdentityPermission(PermissionState.None);
245  urlIdentityPermission2.m_unrestricted = true;
246  return urlIdentityPermission2;
247  }
248  if (m_unrestricted)
249  {
250  return urlIdentityPermission.Copy();
251  }
252  if (urlIdentityPermission.m_unrestricted)
253  {
254  return Copy();
255  }
256  if (m_urls == null || urlIdentityPermission.m_urls == null || m_urls.Length == 0 || urlIdentityPermission.m_urls.Length == 0)
257  {
258  return null;
259  }
260  List<URLString> list = new List<URLString>();
261  URLString[] urls = m_urls;
262  foreach (URLString uRLString in urls)
263  {
264  URLString[] urls2 = urlIdentityPermission.m_urls;
265  foreach (URLString operand in urls2)
266  {
267  URLString uRLString2 = (URLString)uRLString.Intersect(operand);
268  if (uRLString2 != null)
269  {
270  list.Add(uRLString2);
271  }
272  }
273  }
274  if (list.Count == 0)
275  {
276  return null;
277  }
278  UrlIdentityPermission urlIdentityPermission3 = new UrlIdentityPermission(PermissionState.None);
279  urlIdentityPermission3.m_urls = list.ToArray();
280  return urlIdentityPermission3;
281  }
282 
287  public override IPermission Union(IPermission target)
288  {
289  if (target == null)
290  {
291  if ((m_urls == null || m_urls.Length == 0) && !m_unrestricted)
292  {
293  return null;
294  }
295  return Copy();
296  }
297  UrlIdentityPermission urlIdentityPermission = target as UrlIdentityPermission;
298  if (urlIdentityPermission == null)
299  {
300  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
301  }
302  if (m_unrestricted || urlIdentityPermission.m_unrestricted)
303  {
304  UrlIdentityPermission urlIdentityPermission2 = new UrlIdentityPermission(PermissionState.None);
305  urlIdentityPermission2.m_unrestricted = true;
306  return urlIdentityPermission2;
307  }
308  if (m_urls == null || m_urls.Length == 0)
309  {
310  if (urlIdentityPermission.m_urls == null || urlIdentityPermission.m_urls.Length == 0)
311  {
312  return null;
313  }
314  return urlIdentityPermission.Copy();
315  }
316  if (urlIdentityPermission.m_urls == null || urlIdentityPermission.m_urls.Length == 0)
317  {
318  return Copy();
319  }
320  List<URLString> list = new List<URLString>();
321  URLString[] urls = m_urls;
322  foreach (URLString item in urls)
323  {
324  list.Add(item);
325  }
326  URLString[] urls2 = urlIdentityPermission.m_urls;
327  foreach (URLString uRLString in urls2)
328  {
329  bool flag = false;
330  foreach (URLString item2 in list)
331  {
332  if (uRLString.Equals(item2))
333  {
334  flag = true;
335  break;
336  }
337  }
338  if (!flag)
339  {
340  list.Add(uRLString);
341  }
342  }
343  UrlIdentityPermission urlIdentityPermission3 = new UrlIdentityPermission(PermissionState.None);
344  urlIdentityPermission3.m_urls = list.ToArray();
345  return urlIdentityPermission3;
346  }
347 
352  public override void FromXml(SecurityElement esd)
353  {
354  m_unrestricted = false;
355  m_urls = null;
356  CodeAccessPermission.ValidateElement(esd, this);
357  string text = esd.Attribute("Unrestricted");
358  if (text != null && string.Compare(text, "true", StringComparison.OrdinalIgnoreCase) == 0)
359  {
360  m_unrestricted = true;
361  return;
362  }
363  string text2 = esd.Attribute("Url");
364  List<URLString> list = new List<URLString>();
365  if (text2 != null)
366  {
367  list.Add(new URLString(text2, parsed: true));
368  }
369  ArrayList children = esd.Children;
370  if (children != null)
371  {
372  foreach (SecurityElement item in children)
373  {
374  text2 = item.Attribute("Url");
375  if (text2 != null)
376  {
377  list.Add(new URLString(text2, parsed: true));
378  }
379  }
380  }
381  if (list.Count != 0)
382  {
383  m_urls = list.ToArray();
384  }
385  }
386 
389  public override SecurityElement ToXml()
390  {
391  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.UrlIdentityPermission");
392  if (m_unrestricted)
393  {
394  securityElement.AddAttribute("Unrestricted", "true");
395  }
396  else if (m_urls != null)
397  {
398  if (m_urls.Length == 1)
399  {
400  securityElement.AddAttribute("Url", m_urls[0].ToString());
401  }
402  else
403  {
404  for (int i = 0; i < m_urls.Length; i++)
405  {
406  SecurityElement securityElement2 = new SecurityElement("Url");
407  securityElement2.AddAttribute("Url", m_urls[i].ToString());
408  securityElement.AddChild(securityElement2);
409  }
410  }
411  }
412  return securityElement;
413  }
414 
415  int IBuiltInPermission.GetTokenIndex()
416  {
417  return GetTokenIndex();
418  }
419 
420  internal static int GetTokenIndex()
421  {
422  return 13;
423  }
424  }
425 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
StreamingContextStates State
Gets the source or destination of the transmitted data.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
UrlIdentityPermission(string site)
Initializes a new instance of the T:System.Security.Permissions.UrlIdentityPermission class to repres...
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the underlying structure of all code access permissions.
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
UrlIdentityPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.UrlIdentityPermission class with the ...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
The exception that is thrown when one of the arguments provided to a method is not valid.
Defines the identity permission for the URL from which the code originates. This class cannot be inhe...
static SecurityElement FromString(string xml)
Creates a security element from an XML-encoded string.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
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.
ArrayList Children
Gets or sets the array of child elements of the XML element.
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
override IPermission Copy()
Creates and returns an identical copy of the current permission.
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
string Url
Gets or sets a URL representing the identity of Internet code.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14