mscorlib(4.0.0.0) API with additions
SiteIdentityPermission.cs
1 using System.Collections;
5 using System.Security.Util;
6 
8 {
10  [Serializable]
11  [ComVisible(true)]
12  public sealed class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission
13  {
14  [OptionalField(VersionAdded = 2)]
15  private bool m_unrestricted;
16 
17  [OptionalField(VersionAdded = 2)]
18  private SiteString[] m_sites;
19 
20  [OptionalField(VersionAdded = 2)]
21  private string m_serializedPermission;
22 
23  private SiteString m_site;
24 
28  public string Site
29  {
30  get
31  {
32  if (m_sites == null)
33  {
34  return "";
35  }
36  if (m_sites.Length == 1)
37  {
38  return m_sites[0].ToString();
39  }
40  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
41  }
42  set
43  {
44  m_unrestricted = false;
45  m_sites = new SiteString[1];
46  m_sites[0] = new SiteString(value);
47  }
48  }
49 
50  [OnDeserialized]
51  private void OnDeserialized(StreamingContext ctx)
52  {
53  if (m_serializedPermission != null)
54  {
55  FromXml(SecurityElement.FromString(m_serializedPermission));
56  m_serializedPermission = null;
57  }
58  else if (m_site != null)
59  {
60  m_unrestricted = false;
61  m_sites = new SiteString[1];
62  m_sites[0] = m_site;
63  m_site = null;
64  }
65  }
66 
67  [OnSerializing]
68  private void OnSerializing(StreamingContext ctx)
69  {
70  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
71  {
72  m_serializedPermission = ToXml().ToString();
73  if (m_sites != null && m_sites.Length == 1)
74  {
75  m_site = m_sites[0];
76  }
77  }
78  }
79 
80  [OnSerialized]
81  private void OnSerialized(StreamingContext ctx)
82  {
83  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
84  {
85  m_serializedPermission = null;
86  m_site = null;
87  }
88  }
89 
94  {
95  switch (state)
96  {
97  case PermissionState.Unrestricted:
98  m_unrestricted = true;
99  break;
100  case PermissionState.None:
101  m_unrestricted = false;
102  break;
103  default:
104  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
105  }
106  }
107 
111  public SiteIdentityPermission(string site)
112  {
113  Site = site;
114  }
115 
118  public override IPermission Copy()
119  {
120  SiteIdentityPermission siteIdentityPermission = new SiteIdentityPermission(PermissionState.None);
121  siteIdentityPermission.m_unrestricted = m_unrestricted;
122  if (m_sites != null)
123  {
124  siteIdentityPermission.m_sites = new SiteString[m_sites.Length];
125  for (int i = 0; i < m_sites.Length; i++)
126  {
127  siteIdentityPermission.m_sites[i] = m_sites[i].Copy();
128  }
129  }
130  return siteIdentityPermission;
131  }
132 
138  public override bool IsSubsetOf(IPermission target)
139  {
140  if (target == null)
141  {
142  if (m_unrestricted)
143  {
144  return false;
145  }
146  if (m_sites == null)
147  {
148  return true;
149  }
150  if (m_sites.Length == 0)
151  {
152  return true;
153  }
154  return false;
155  }
156  SiteIdentityPermission siteIdentityPermission = target as SiteIdentityPermission;
157  if (siteIdentityPermission == null)
158  {
159  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
160  }
161  if (siteIdentityPermission.m_unrestricted)
162  {
163  return true;
164  }
165  if (m_unrestricted)
166  {
167  return false;
168  }
169  if (m_sites != null)
170  {
171  SiteString[] sites = m_sites;
172  foreach (SiteString siteString in sites)
173  {
174  bool flag = false;
175  if (siteIdentityPermission.m_sites != null)
176  {
177  SiteString[] sites2 = siteIdentityPermission.m_sites;
178  foreach (SiteString operand in sites2)
179  {
180  if (siteString.IsSubsetOf(operand))
181  {
182  flag = true;
183  break;
184  }
185  }
186  }
187  if (!flag)
188  {
189  return false;
190  }
191  }
192  }
193  return true;
194  }
195 
200  public override IPermission Intersect(IPermission target)
201  {
202  if (target == null)
203  {
204  return null;
205  }
206  SiteIdentityPermission siteIdentityPermission = target as SiteIdentityPermission;
207  if (siteIdentityPermission == null)
208  {
209  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
210  }
211  if (m_unrestricted && siteIdentityPermission.m_unrestricted)
212  {
213  SiteIdentityPermission siteIdentityPermission2 = new SiteIdentityPermission(PermissionState.None);
214  siteIdentityPermission2.m_unrestricted = true;
215  return siteIdentityPermission2;
216  }
217  if (m_unrestricted)
218  {
219  return siteIdentityPermission.Copy();
220  }
221  if (siteIdentityPermission.m_unrestricted)
222  {
223  return Copy();
224  }
225  if (m_sites == null || siteIdentityPermission.m_sites == null || m_sites.Length == 0 || siteIdentityPermission.m_sites.Length == 0)
226  {
227  return null;
228  }
229  List<SiteString> list = new List<SiteString>();
230  SiteString[] sites = m_sites;
231  foreach (SiteString siteString in sites)
232  {
233  SiteString[] sites2 = siteIdentityPermission.m_sites;
234  foreach (SiteString operand in sites2)
235  {
236  SiteString siteString2 = siteString.Intersect(operand);
237  if (siteString2 != null)
238  {
239  list.Add(siteString2);
240  }
241  }
242  }
243  if (list.Count == 0)
244  {
245  return null;
246  }
247  SiteIdentityPermission siteIdentityPermission3 = new SiteIdentityPermission(PermissionState.None);
248  siteIdentityPermission3.m_sites = list.ToArray();
249  return siteIdentityPermission3;
250  }
251 
256  public override IPermission Union(IPermission target)
257  {
258  if (target == null)
259  {
260  if ((m_sites == null || m_sites.Length == 0) && !m_unrestricted)
261  {
262  return null;
263  }
264  return Copy();
265  }
266  SiteIdentityPermission siteIdentityPermission = target as SiteIdentityPermission;
267  if (siteIdentityPermission == null)
268  {
269  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
270  }
271  if (m_unrestricted || siteIdentityPermission.m_unrestricted)
272  {
273  SiteIdentityPermission siteIdentityPermission2 = new SiteIdentityPermission(PermissionState.None);
274  siteIdentityPermission2.m_unrestricted = true;
275  return siteIdentityPermission2;
276  }
277  if (m_sites == null || m_sites.Length == 0)
278  {
279  if (siteIdentityPermission.m_sites == null || siteIdentityPermission.m_sites.Length == 0)
280  {
281  return null;
282  }
283  return siteIdentityPermission.Copy();
284  }
285  if (siteIdentityPermission.m_sites == null || siteIdentityPermission.m_sites.Length == 0)
286  {
287  return Copy();
288  }
289  List<SiteString> list = new List<SiteString>();
290  SiteString[] sites = m_sites;
291  foreach (SiteString item in sites)
292  {
293  list.Add(item);
294  }
295  SiteString[] sites2 = siteIdentityPermission.m_sites;
296  foreach (SiteString siteString in sites2)
297  {
298  bool flag = false;
299  foreach (SiteString item2 in list)
300  {
301  if (siteString.Equals(item2))
302  {
303  flag = true;
304  break;
305  }
306  }
307  if (!flag)
308  {
309  list.Add(siteString);
310  }
311  }
312  SiteIdentityPermission siteIdentityPermission3 = new SiteIdentityPermission(PermissionState.None);
313  siteIdentityPermission3.m_sites = list.ToArray();
314  return siteIdentityPermission3;
315  }
316 
321  public override void FromXml(SecurityElement esd)
322  {
323  m_unrestricted = false;
324  m_sites = null;
325  CodeAccessPermission.ValidateElement(esd, this);
326  string text = esd.Attribute("Unrestricted");
327  if (text != null && string.Compare(text, "true", StringComparison.OrdinalIgnoreCase) == 0)
328  {
329  m_unrestricted = true;
330  return;
331  }
332  string text2 = esd.Attribute("Site");
333  List<SiteString> list = new List<SiteString>();
334  if (text2 != null)
335  {
336  list.Add(new SiteString(text2));
337  }
338  ArrayList children = esd.Children;
339  if (children != null)
340  {
341  foreach (SecurityElement item in children)
342  {
343  text2 = item.Attribute("Site");
344  if (text2 != null)
345  {
346  list.Add(new SiteString(text2));
347  }
348  }
349  }
350  if (list.Count != 0)
351  {
352  m_sites = list.ToArray();
353  }
354  }
355 
358  public override SecurityElement ToXml()
359  {
360  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.SiteIdentityPermission");
361  if (m_unrestricted)
362  {
363  securityElement.AddAttribute("Unrestricted", "true");
364  }
365  else if (m_sites != null)
366  {
367  if (m_sites.Length == 1)
368  {
369  securityElement.AddAttribute("Site", m_sites[0].ToString());
370  }
371  else
372  {
373  for (int i = 0; i < m_sites.Length; i++)
374  {
375  SecurityElement securityElement2 = new SecurityElement("Site");
376  securityElement2.AddAttribute("Site", m_sites[i].ToString());
377  securityElement.AddChild(securityElement2);
378  }
379  }
380  }
381  return securityElement;
382  }
383 
384  int IBuiltInPermission.GetTokenIndex()
385  {
386  return GetTokenIndex();
387  }
388 
389  internal static int GetTokenIndex()
390  {
391  return 11;
392  }
393  }
394 }
override IPermission Copy()
Creates and returns an identical copy of the current permission.
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 SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
SiteIdentityPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.SiteIdentityPermission class with the...
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.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
Defines the underlying structure of all code access permissions.
Defines the identity permission for the Web site from which the code originates. This class cannot be...
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
The exception that is thrown when one of the arguments provided to a method is not valid.
static SecurityElement FromString(string xml)
Creates a security element from an XML-encoded string.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
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 string ToString()
Produces a string representation of an XML element and its constituent attributes,...
SiteIdentityPermission(string site)
Initializes a new instance of the T:System.Security.Permissions.SiteIdentityPermission class to repre...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14