10 [SecurityPermission(
SecurityAction.InheritanceDemand, ControlEvidence =
true, ControlPolicy =
true)]
13 [SuppressUnmanagedCodeSecurity]
14 private static class UnsafeNativeMethods
16 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Auto)]
17 internal static extern bool GetComputerName(
StringBuilder lpBuffer, ref
int nSize);
20 private static volatile string computerName;
22 private string[] tagNames;
24 private Type permissionAccessType;
26 private bool isUnrestricted;
28 private Hashtable rootTable = CreateHashtable();
31 public const string Any =
"*";
34 public const string Local =
".";
36 private string ComputerName
40 if (computerName ==
null)
44 if (computerName ==
null)
48 UnsafeNativeMethods.GetComputerName(stringBuilder, ref nSize);
49 computerName = stringBuilder.
ToString();
63 return rootTable.
Count == 0;
77 return permissionAccessType;
89 permissionAccessType = value;
109 if (value.Length == 0)
111 throw new ArgumentException(SR.GetString(
"PermissionInvalidLength",
"0"),
"value");
130 isUnrestricted =
true;
133 isUnrestricted =
false;
140 private static Hashtable CreateHashtable()
161 for (
int i = 0; i < permissionAccessPath.Length - 1; i++)
163 if (hashtable.
ContainsKey(permissionAccessPath[i]))
165 hashtable = (
Hashtable)hashtable[permissionAccessPath[i]];
168 Hashtable hashtable2 = CreateHashtable();
169 hashtable[permissionAccessPath[i]] = hashtable2;
170 hashtable = hashtable2;
172 if (hashtable.
ContainsKey(permissionAccessPath[permissionAccessPath.Length - 1]))
176 hashtable[permissionAccessPath[permissionAccessPath.Length - 1]] = entry.
PermissionAccess;
190 resourcePermissionBase.tagNames = tagNames;
191 resourcePermissionBase.permissionAccessType = permissionAccessType;
192 resourcePermissionBase.isUnrestricted = isUnrestricted;
193 resourcePermissionBase.rootTable = CopyChildren(rootTable, 0);
194 return resourcePermissionBase;
197 private Hashtable CopyChildren(
object currentContent,
int tagIndex)
205 hashtable[enumerator.
Key] = CopyChildren(enumerator.
Value, tagIndex + 1);
209 hashtable[enumerator.
Key] = enumerator.
Value;
225 return GetChildrenAccess(rootTable, 0);
237 for (
int i = 0; i < childrenAccess.Length; i++)
245 ResourcePermissionBaseEntry resourcePermissionBaseEntry =
new ResourcePermissionBaseEntry((
int)enumerator.
Value,
new string[
TagNames.Length]);
246 resourcePermissionBaseEntry.PermissionAccessPath[tagIndex] = (string)enumerator.
Key;
247 arrayList.
Add(resourcePermissionBaseEntry);
250 return (ResourcePermissionBaseEntry[])arrayList.
ToArray(typeof(ResourcePermissionBaseEntry));
259 if (securityElement ==
null)
263 if (!securityElement.
Tag.Equals(
"Permission") && !securityElement.
Tag.Equals(
"IPermission"))
267 string text = securityElement.Attribute(
"version");
268 if (text !=
null && !text.Equals(
"1"))
272 string text2 = securityElement.Attribute(
"Unrestricted");
273 if (text2 !=
null &&
string.Compare(text2,
"true",
StringComparison.OrdinalIgnoreCase) == 0)
275 isUnrestricted =
true;
278 isUnrestricted =
false;
279 rootTable = (
Hashtable)ReadChildren(securityElement, 0);
292 if (target.GetType() != GetType())
299 return resourcePermissionBase.
Copy();
306 Hashtable hashtable = (
Hashtable)IntersectContents(rootTable, resourcePermissionBase.rootTable);
307 if (hashtable !=
null)
309 resourcePermissionBase2 = CreateInstance();
310 resourcePermissionBase2.rootTable = hashtable;
312 return resourcePermissionBase2;
315 private object IntersectContents(
object currentContent,
object targetContent)
317 if (currentContent is
int)
319 int num = (int)currentContent;
320 int num2 = (int)targetContent;
324 object obj = ((
Hashtable)currentContent)[
"."];
325 object obj2 = ((
Hashtable)currentContent)[ComputerName];
326 if (obj !=
null || obj2 !=
null)
328 object obj3 = ((
Hashtable)targetContent)[
"."];
329 object obj4 = ((
Hashtable)targetContent)[ComputerName];
330 if (obj3 !=
null || obj4 !=
null)
332 object currentContent2 = obj;
333 if (obj !=
null && obj2 !=
null)
335 currentContent2 = UnionOfContents(obj, obj2);
337 else if (obj2 !=
null)
339 currentContent2 = obj2;
341 object targetContent2 = obj3;
342 if (obj3 !=
null && obj4 !=
null)
344 targetContent2 = UnionOfContents(obj3, obj4);
346 else if (obj4 !=
null)
348 targetContent2 = obj4;
350 object value = IntersectContents(currentContent2, targetContent2);
351 if (HasContent(value))
353 if (obj2 !=
null || obj4 !=
null)
355 hashtable[ComputerName] = value;
359 hashtable[
"."] = value;
368 enumerator = ((
Hashtable)currentContent).GetEnumerator();
373 enumerator = ((
Hashtable)targetContent).GetEnumerator();
378 string text = (string)enumerator.
Key;
379 if (hashtable2.
ContainsKey(text) && text !=
"." && text != ComputerName)
381 object value2 = enumerator.
Value;
382 object targetContent3 = hashtable2[text];
383 object value3 = IntersectContents(value2, targetContent3);
384 if (HasContent(value3))
386 hashtable[text] = value3;
390 if (hashtable.
Count <= 0)
397 private bool HasContent(
object value)
405 int num = (int)value;
412 if (HasContent(enumerator.
Value))
420 private bool IsContentSubset(
object currentContent,
object targetContent)
422 if (currentContent is
int)
424 int num = (int)currentContent;
425 int num2 = (int)targetContent;
426 if ((num & num2) != num)
434 object obj = hashtable2[
"*"];
439 if (!IsContentSubset(item.Value, obj))
448 string text = (string)item2.
Key;
449 if (HasContent(item2.
Value) && text !=
"." && text != ComputerName)
455 if (!IsContentSubset(item2.
Value, hashtable2[text]))
461 object obj2 = MergeContents(hashtable[
"."], hashtable[ComputerName]);
464 object obj3 = MergeContents(hashtable2[
"."], hashtable2[ComputerName]);
467 return IsContentSubset(obj2, obj3);
477 private object MergeContents(
object content1,
object content2)
479 if (content1 ==
null)
481 if (content2 ==
null)
487 if (content2 ==
null)
491 return UnionOfContents(content1, content2);
504 if (target.GetType() != GetType())
517 return IsContentSubset(rootTable, resourcePermissionBase.rootTable);
525 return isUnrestricted;
528 private object ReadChildren(
SecurityElement securityElement,
int tagIndex)
531 if (securityElement.
Children !=
null)
540 string key = securityElement2.Attribute(
"name");
543 hashtable[key] = ReadChildren(securityElement2, tagIndex + 1);
546 string text = securityElement2.Attribute(
"access");
552 hashtable[key] = num;
577 if (num >= permissionAccessPath.Length)
581 if (hashtable ==
null || !hashtable.
ContainsKey(permissionAccessPath[num]))
586 if (num < permissionAccessPath.Length - 1)
588 hashtable = (
Hashtable)hashtable[permissionAccessPath[num]];
589 if (hashtable.
Count == 1)
591 hashtable2.
Remove(permissionAccessPath[num]);
597 hashtable2.
Remove(permissionAccessPath[num]);
609 Type type = GetType();
615 return securityElement;
617 WriteChildren(securityElement, rootTable, 0);
618 return securityElement;
631 if (target.GetType() != GetType())
639 resourcePermissionBase2 = CreateInstance();
640 resourcePermissionBase2.isUnrestricted =
true;
644 Hashtable hashtable = (
Hashtable)UnionOfContents(rootTable, resourcePermissionBase.rootTable);
645 if (hashtable !=
null)
647 resourcePermissionBase2 = CreateInstance();
648 resourcePermissionBase2.rootTable = hashtable;
651 return resourcePermissionBase2;
654 private object UnionOfContents(
object currentContent,
object targetContent)
656 if (currentContent is
int)
658 int num = (int)currentContent;
659 int num2 = (int)targetContent;
667 hashtable[(string)enumerator.
Key] = enumerator.
Value;
673 hashtable[enumerator2.
Key] = enumerator2.
Value;
676 object currentContent2 = hashtable[enumerator2.
Key];
677 object value = enumerator2.
Value;
678 hashtable[enumerator2.
Key] = UnionOfContents(currentContent2, value);
680 if (hashtable.
Count <= 0)
687 private void WriteChildren(SecurityElement currentElement,
object currentContent,
int tagIndex)
692 SecurityElement securityElement =
new SecurityElement(
TagNames[tagIndex]);
693 currentElement.AddChild(securityElement);
694 securityElement.AddAttribute(
"name", (
string)enumerator.
Key);
697 WriteChildren(securityElement, enumerator.
Value, tagIndex + 1);
701 int num = (int)enumerator.
Value;
705 securityElement.AddAttribute(
"access", text);
virtual Assembly Assembly
Gets the appropriate T:System.Reflection.Assembly for this instance of T:System.Reflection....
Allows a permission to expose an unrestricted state.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Clear()
Clears the permission of the added permission entries.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission object is a subset of the specified permission.
bool MoveNext()
Advances the enumerator to the next element of the collection.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
virtual string FullName
Gets the display name of the assembly.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
object Key
Gets the key of the current dictionary entry.
const string Local
Specifies the character to be used to represent a local reference.
const string Any
Specifies the character to be used to represent the any wildcard character.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
override SecurityElement ToXml()
Creates and returns an XML encoding of the security object and its current state.
virtual bool ContainsKey(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
override IPermission Union(IPermission target)
Creates a permission object that combines the current permission object and the target permission obj...
string Tag
Gets or sets the tag name of an XML element.
Allows control of code access security permissions.
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
string [] TagNames
Gets or sets an array of strings that identify the resource you are protecting.
int PermissionAccess
Gets an integer representation of the access level enumeration value.
SecurityAction
Specifies the security actions that can be performed using declarative security.
bool IsUnrestricted()
Gets a value indicating whether the permission is unrestricted.
Provides the base class for enumerations.
virtual void AddRange(ICollection c)
Adds the elements of an T:System.Collections.ICollection to the end of the T:System....
Represents the XML object model for encoding security objects. This class cannot be inherited.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Defines the underlying structure of all code access permissions.
ResourcePermissionBase()
Initializes a new instance of the T:System.Security.Permissions.ResourcePermissionBase class.
override IPermission Copy()
Creates and returns an identical copy of the current permission object.
Represents type declarations: class types, interface types, array types, value types,...
override void FromXml(SecurityElement securityElement)
Reconstructs a security object with a specified state from an XML encoding.
CharSet
Dictates which character set marshaled strings should use.
Defines methods implemented by permission types.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
void RemovePermissionAccess(ResourcePermissionBaseEntry entry)
Removes a permission entry from the permission.
The exception that is thrown when one of the arguments provided to a method is not valid.
int Capacity
Gets or sets the maximum number of characters that can be contained in the memory allocated by the cu...
Attribute can be applied to an enumeration.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
object Value
Gets or sets the value in the key/value pair.
object Value
Gets the value of the current dictionary entry.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Defines the smallest unit of a code access security permission set.
Specifies that the class can be serialized.
Enumerates the elements of a nongeneric dictionary.
ArrayList Children
Gets or sets the array of child elements of the XML element.
Type PermissionAccessType
Gets or sets an enumeration value that describes the types of access that you are giving the resource...
static StringComparer OrdinalIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive ordinal string comparison.
The exception that is thrown when a method call is invalid for the object's current state.
void AddPermissionAccess(ResourcePermissionBaseEntry entry)
Adds a permission entry to the permission.
object Key
Gets or sets the key in the key/value pair.
string [] PermissionAccessPath
Gets an array of strings that identify the resource you are protecting.
virtual void Clear()
Removes all elements from the T:System.Collections.Hashtable.
abstract new Module Module
Gets the module (the DLL) in which the current T:System.Type is defined.
override IPermission Intersect(IPermission target)
Creates and returns a permission object that is the intersection of the current permission object and...
Defines a dictionary key/value pair that can be set or retrieved.
ResourcePermissionBase(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.ResourcePermissionBase class with the...
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
ResourcePermissionBaseEntry [] GetPermissionEntries()
Returns an array of the T:System.Security.Permissions.ResourcePermissionBaseEntry objects added to th...