mscorlib(4.0.0.0) API with additions
StrongNameIdentityPermission.cs
1 using System.Collections;
4 using System.Security.Util;
5 
7 {
10  [ComVisible(true)]
11  public sealed class StrongNameIdentityPermission : CodeAccessPermission, IBuiltInPermission
12  {
13  private bool m_unrestricted;
14 
15  private StrongName2[] m_strongNames;
16 
22  {
23  get
24  {
25  if (m_strongNames == null || m_strongNames.Length == 0)
26  {
27  return null;
28  }
29  if (m_strongNames.Length > 1)
30  {
31  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
32  }
33  return m_strongNames[0].m_publicKeyBlob;
34  }
35  set
36  {
37  if (value == null)
38  {
39  throw new ArgumentNullException("PublicKey");
40  }
41  m_unrestricted = false;
42  if (m_strongNames != null && m_strongNames.Length == 1)
43  {
44  m_strongNames[0].m_publicKeyBlob = value;
45  return;
46  }
47  m_strongNames = new StrongName2[1];
48  m_strongNames[0] = new StrongName2(value, "", new Version());
49  }
50  }
51 
56  public string Name
57  {
58  get
59  {
60  if (m_strongNames == null || m_strongNames.Length == 0)
61  {
62  return "";
63  }
64  if (m_strongNames.Length > 1)
65  {
66  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
67  }
68  return m_strongNames[0].m_name;
69  }
70  set
71  {
72  if (value != null && value.Length == 0)
73  {
74  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"));
75  }
76  m_unrestricted = false;
77  if (m_strongNames != null && m_strongNames.Length == 1)
78  {
79  m_strongNames[0].m_name = value;
80  return;
81  }
82  m_strongNames = new StrongName2[1];
83  m_strongNames[0] = new StrongName2(null, value, new Version());
84  }
85  }
86 
90  public Version Version
91  {
92  get
93  {
94  if (m_strongNames == null || m_strongNames.Length == 0)
95  {
96  return new Version();
97  }
98  if (m_strongNames.Length > 1)
99  {
100  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
101  }
102  return m_strongNames[0].m_version;
103  }
104  set
105  {
106  m_unrestricted = false;
107  if (m_strongNames != null && m_strongNames.Length == 1)
108  {
109  m_strongNames[0].m_version = value;
110  return;
111  }
112  m_strongNames = new StrongName2[1];
113  m_strongNames[0] = new StrongName2(null, "", value);
114  }
115  }
116 
121  {
122  switch (state)
123  {
124  case PermissionState.Unrestricted:
125  m_unrestricted = true;
126  break;
127  case PermissionState.None:
128  m_unrestricted = false;
129  break;
130  default:
131  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
132  }
133  }
134 
142  {
143  if (blob == null)
144  {
145  throw new ArgumentNullException("blob");
146  }
147  if (name != null && name.Equals(""))
148  {
149  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyStrongName"));
150  }
151  m_unrestricted = false;
152  m_strongNames = new StrongName2[1];
153  m_strongNames[0] = new StrongName2(blob, name, version);
154  }
155 
158  public override IPermission Copy()
159  {
160  StrongNameIdentityPermission strongNameIdentityPermission = new StrongNameIdentityPermission(PermissionState.None);
161  strongNameIdentityPermission.m_unrestricted = m_unrestricted;
162  if (m_strongNames != null)
163  {
164  strongNameIdentityPermission.m_strongNames = new StrongName2[m_strongNames.Length];
165  for (int i = 0; i < m_strongNames.Length; i++)
166  {
167  strongNameIdentityPermission.m_strongNames[i] = m_strongNames[i].Copy();
168  }
169  }
170  return strongNameIdentityPermission;
171  }
172 
178  public override bool IsSubsetOf(IPermission target)
179  {
180  if (target == null)
181  {
182  if (m_unrestricted)
183  {
184  return false;
185  }
186  if (m_strongNames == null)
187  {
188  return true;
189  }
190  if (m_strongNames.Length == 0)
191  {
192  return true;
193  }
194  return false;
195  }
196  StrongNameIdentityPermission strongNameIdentityPermission = target as StrongNameIdentityPermission;
197  if (strongNameIdentityPermission == null)
198  {
199  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
200  }
201  if (strongNameIdentityPermission.m_unrestricted)
202  {
203  return true;
204  }
205  if (m_unrestricted)
206  {
207  return false;
208  }
209  if (m_strongNames != null)
210  {
211  StrongName2[] strongNames = m_strongNames;
212  foreach (StrongName2 strongName in strongNames)
213  {
214  bool flag = false;
215  if (strongNameIdentityPermission.m_strongNames != null)
216  {
217  StrongName2[] strongNames2 = strongNameIdentityPermission.m_strongNames;
218  foreach (StrongName2 target2 in strongNames2)
219  {
220  if (strongName.IsSubsetOf(target2))
221  {
222  flag = true;
223  break;
224  }
225  }
226  }
227  if (!flag)
228  {
229  return false;
230  }
231  }
232  }
233  return true;
234  }
235 
240  public override IPermission Intersect(IPermission target)
241  {
242  if (target == null)
243  {
244  return null;
245  }
246  StrongNameIdentityPermission strongNameIdentityPermission = target as StrongNameIdentityPermission;
247  if (strongNameIdentityPermission == null)
248  {
249  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
250  }
251  if (m_unrestricted && strongNameIdentityPermission.m_unrestricted)
252  {
253  StrongNameIdentityPermission strongNameIdentityPermission2 = new StrongNameIdentityPermission(PermissionState.None);
254  strongNameIdentityPermission2.m_unrestricted = true;
255  return strongNameIdentityPermission2;
256  }
257  if (m_unrestricted)
258  {
259  return strongNameIdentityPermission.Copy();
260  }
261  if (strongNameIdentityPermission.m_unrestricted)
262  {
263  return Copy();
264  }
265  if (m_strongNames == null || strongNameIdentityPermission.m_strongNames == null || m_strongNames.Length == 0 || strongNameIdentityPermission.m_strongNames.Length == 0)
266  {
267  return null;
268  }
270  StrongName2[] strongNames = m_strongNames;
271  foreach (StrongName2 strongName in strongNames)
272  {
273  StrongName2[] strongNames2 = strongNameIdentityPermission.m_strongNames;
274  foreach (StrongName2 target2 in strongNames2)
275  {
276  StrongName2 strongName2 = strongName.Intersect(target2);
277  if (strongName2 != null)
278  {
279  list.Add(strongName2);
280  }
281  }
282  }
283  if (list.Count == 0)
284  {
285  return null;
286  }
287  StrongNameIdentityPermission strongNameIdentityPermission3 = new StrongNameIdentityPermission(PermissionState.None);
288  strongNameIdentityPermission3.m_strongNames = list.ToArray();
289  return strongNameIdentityPermission3;
290  }
291 
296  public override IPermission Union(IPermission target)
297  {
298  if (target == null)
299  {
300  if ((m_strongNames == null || m_strongNames.Length == 0) && !m_unrestricted)
301  {
302  return null;
303  }
304  return Copy();
305  }
306  StrongNameIdentityPermission strongNameIdentityPermission = target as StrongNameIdentityPermission;
307  if (strongNameIdentityPermission == null)
308  {
309  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
310  }
311  if (m_unrestricted || strongNameIdentityPermission.m_unrestricted)
312  {
313  StrongNameIdentityPermission strongNameIdentityPermission2 = new StrongNameIdentityPermission(PermissionState.None);
314  strongNameIdentityPermission2.m_unrestricted = true;
315  return strongNameIdentityPermission2;
316  }
317  if (m_strongNames == null || m_strongNames.Length == 0)
318  {
319  if (strongNameIdentityPermission.m_strongNames == null || strongNameIdentityPermission.m_strongNames.Length == 0)
320  {
321  return null;
322  }
323  return strongNameIdentityPermission.Copy();
324  }
325  if (strongNameIdentityPermission.m_strongNames == null || strongNameIdentityPermission.m_strongNames.Length == 0)
326  {
327  return Copy();
328  }
330  StrongName2[] strongNames = m_strongNames;
331  foreach (StrongName2 item in strongNames)
332  {
333  list.Add(item);
334  }
335  StrongName2[] strongNames2 = strongNameIdentityPermission.m_strongNames;
336  foreach (StrongName2 strongName in strongNames2)
337  {
338  bool flag = false;
339  foreach (StrongName2 item2 in list)
340  {
341  if (strongName.Equals(item2))
342  {
343  flag = true;
344  break;
345  }
346  }
347  if (!flag)
348  {
349  list.Add(strongName);
350  }
351  }
352  StrongNameIdentityPermission strongNameIdentityPermission3 = new StrongNameIdentityPermission(PermissionState.None);
353  strongNameIdentityPermission3.m_strongNames = list.ToArray();
354  return strongNameIdentityPermission3;
355  }
356 
361  public override void FromXml(SecurityElement e)
362  {
363  m_unrestricted = false;
364  m_strongNames = null;
365  CodeAccessPermission.ValidateElement(e, this);
366  string text = e.Attribute("Unrestricted");
367  if (text != null && string.Compare(text, "true", StringComparison.OrdinalIgnoreCase) == 0)
368  {
369  m_unrestricted = true;
370  return;
371  }
372  string text2 = e.Attribute("PublicKeyBlob");
373  string text3 = e.Attribute("Name");
374  string text4 = e.Attribute("AssemblyVersion");
376  if (text2 != null || text3 != null || text4 != null)
377  {
378  StrongName2 item = new StrongName2((text2 == null) ? null : new StrongNamePublicKeyBlob(text2), text3, (text4 == null) ? null : new Version(text4));
379  list.Add(item);
380  }
381  ArrayList children = e.Children;
382  if (children != null)
383  {
384  foreach (SecurityElement item2 in children)
385  {
386  text2 = item2.Attribute("PublicKeyBlob");
387  text3 = item2.Attribute("Name");
388  text4 = item2.Attribute("AssemblyVersion");
389  if (text2 != null || text3 != null || text4 != null)
390  {
391  StrongName2 item = new StrongName2((text2 == null) ? null : new StrongNamePublicKeyBlob(text2), text3, (text4 == null) ? null : new Version(text4));
392  list.Add(item);
393  }
394  }
395  }
396  if (list.Count != 0)
397  {
398  m_strongNames = list.ToArray();
399  }
400  }
401 
404  public override SecurityElement ToXml()
405  {
406  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.StrongNameIdentityPermission");
407  if (m_unrestricted)
408  {
409  securityElement.AddAttribute("Unrestricted", "true");
410  }
411  else if (m_strongNames != null)
412  {
413  if (m_strongNames.Length == 1)
414  {
415  if (m_strongNames[0].m_publicKeyBlob != null)
416  {
417  securityElement.AddAttribute("PublicKeyBlob", Hex.EncodeHexString(m_strongNames[0].m_publicKeyBlob.PublicKey));
418  }
419  if (m_strongNames[0].m_name != null)
420  {
421  securityElement.AddAttribute("Name", m_strongNames[0].m_name);
422  }
423  if ((object)m_strongNames[0].m_version != null)
424  {
425  securityElement.AddAttribute("AssemblyVersion", m_strongNames[0].m_version.ToString());
426  }
427  }
428  else
429  {
430  for (int i = 0; i < m_strongNames.Length; i++)
431  {
432  SecurityElement securityElement2 = new SecurityElement("StrongName");
433  if (m_strongNames[i].m_publicKeyBlob != null)
434  {
435  securityElement2.AddAttribute("PublicKeyBlob", Hex.EncodeHexString(m_strongNames[i].m_publicKeyBlob.PublicKey));
436  }
437  if (m_strongNames[i].m_name != null)
438  {
439  securityElement2.AddAttribute("Name", m_strongNames[i].m_name);
440  }
441  if ((object)m_strongNames[i].m_version != null)
442  {
443  securityElement2.AddAttribute("AssemblyVersion", m_strongNames[i].m_version.ToString());
444  }
445  securityElement.AddChild(securityElement2);
446  }
447  }
448  }
449  return securityElement;
450  }
451 
452  int IBuiltInPermission.GetTokenIndex()
453  {
454  return GetTokenIndex();
455  }
456 
457  internal static int GetTokenIndex()
458  {
459  return 12;
460  }
461  }
462 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
override IPermission Copy()
Creates and returns an identical copy of the current permission.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
StrongNamePublicKeyBlob PublicKey
Gets or sets the public key blob that defines the strong name identity namespace.
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.
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
string Name
Gets or sets the simple name portion of the strong name identity.
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
StrongNameIdentityPermission(StrongNamePublicKeyBlob blob, string name, Version version)
Initializes a new instance of the T:System.Security.Permissions.StrongNameIdentityPermission class fo...
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the underlying structure of all code access permissions.
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
Defines methods implemented by permission types.
Definition: IPermission.cs:7
Defines the identity permission for strong names. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
Version Version
Gets or sets the version number of the identity.
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.
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.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
override void FromXml(SecurityElement e)
Reconstructs a permission with a specified state from an XML encoding.
Represents the public key information (called a blob) for a strong name. This class cannot be inherit...
StrongNameIdentityPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.StrongNameIdentityPermission class wi...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14