mscorlib(4.0.0.0) API with additions
NetworkInformationPermission.cs
1 using System.Security;
3 
5 {
9  {
10  private NetworkInformationAccess access;
11 
12  private bool unrestricted;
13 
16  public NetworkInformationAccess Access => access;
17 
21  {
22  if (state == PermissionState.Unrestricted)
23  {
25  unrestricted = true;
26  }
27  else
28  {
29  access = NetworkInformationAccess.None;
30  }
31  }
32 
33  internal NetworkInformationPermission(bool unrestricted)
34  {
35  if (unrestricted)
36  {
38  unrestricted = true;
39  }
40  else
41  {
42  access = NetworkInformationAccess.None;
43  }
44  }
45 
49  {
50  this.access = access;
51  }
52 
56  {
57  this.access |= access;
58  }
59 
63  public bool IsUnrestricted()
64  {
65  return unrestricted;
66  }
67 
70  public override IPermission Copy()
71  {
72  if (unrestricted)
73  {
74  return new NetworkInformationPermission(unrestricted: true);
75  }
76  return new NetworkInformationPermission(access);
77  }
78 
82  public override IPermission Union(IPermission target)
83  {
84  if (target == null)
85  {
86  return Copy();
87  }
88  NetworkInformationPermission networkInformationPermission = target as NetworkInformationPermission;
89  if (networkInformationPermission == null)
90  {
91  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
92  }
93  if (unrestricted || networkInformationPermission.IsUnrestricted())
94  {
95  return new NetworkInformationPermission(unrestricted: true);
96  }
97  return new NetworkInformationPermission(access | networkInformationPermission.access);
98  }
99 
105  public override IPermission Intersect(IPermission target)
106  {
107  if (target == null)
108  {
109  return null;
110  }
111  NetworkInformationPermission networkInformationPermission = target as NetworkInformationPermission;
112  if (networkInformationPermission == null)
113  {
114  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
115  }
116  if (unrestricted && networkInformationPermission.IsUnrestricted())
117  {
118  return new NetworkInformationPermission(unrestricted: true);
119  }
120  return new NetworkInformationPermission(access & networkInformationPermission.access);
121  }
122 
127  public override bool IsSubsetOf(IPermission target)
128  {
129  if (target == null)
130  {
131  return access == NetworkInformationAccess.None;
132  }
133  NetworkInformationPermission networkInformationPermission = target as NetworkInformationPermission;
134  if (networkInformationPermission == null)
135  {
136  throw new ArgumentException(SR.GetString("net_perm_target"), "target");
137  }
138  if (unrestricted && !networkInformationPermission.IsUnrestricted())
139  {
140  return false;
141  }
142  if ((access & networkInformationPermission.access) == access)
143  {
144  return true;
145  }
146  return false;
147  }
148 
157  public override void FromXml(SecurityElement securityElement)
158  {
159  access = NetworkInformationAccess.None;
160  if (securityElement == null)
161  {
162  throw new ArgumentNullException("securityElement");
163  }
164  if (!securityElement.Tag.Equals("IPermission"))
165  {
166  throw new ArgumentException(SR.GetString("net_not_ipermission"), "securityElement");
167  }
168  string text = securityElement.Attribute("class");
169  if (text == null)
170  {
171  throw new ArgumentException(SR.GetString("net_no_classname"), "securityElement");
172  }
173  if (text.IndexOf(GetType().FullName) < 0)
174  {
175  throw new ArgumentException(SR.GetString("net_no_typename"), "securityElement");
176  }
177  string text2 = securityElement.Attribute("Unrestricted");
178  if (text2 != null && string.Compare(text2, "true", StringComparison.OrdinalIgnoreCase) == 0)
179  {
180  access = (NetworkInformationAccess.Read | NetworkInformationAccess.Ping);
181  unrestricted = true;
182  }
183  else if (securityElement.Children != null)
184  {
185  foreach (SecurityElement child in securityElement.Children)
186  {
187  text2 = child.Attribute("Access");
188  if (string.Compare(text2, "Read", StringComparison.OrdinalIgnoreCase) == 0)
189  {
190  access |= NetworkInformationAccess.Read;
191  }
192  else if (string.Compare(text2, "Ping", StringComparison.OrdinalIgnoreCase) == 0)
193  {
194  access |= NetworkInformationAccess.Ping;
195  }
196  }
197  }
198  }
199 
202  public override SecurityElement ToXml()
203  {
204  SecurityElement securityElement = new SecurityElement("IPermission");
205  securityElement.AddAttribute("class", GetType().FullName + ", " + GetType().Module.Assembly.FullName.Replace('"', '\''));
206  securityElement.AddAttribute("version", "1");
207  if (unrestricted)
208  {
209  securityElement.AddAttribute("Unrestricted", "true");
210  return securityElement;
211  }
212  if ((access & NetworkInformationAccess.Read) > NetworkInformationAccess.None)
213  {
214  SecurityElement securityElement2 = new SecurityElement("NetworkInformationAccess");
215  securityElement2.AddAttribute("Access", "Read");
216  securityElement.AddChild(securityElement2);
217  }
218  if ((access & NetworkInformationAccess.Ping) > NetworkInformationAccess.None)
219  {
220  SecurityElement securityElement3 = new SecurityElement("NetworkInformationAccess");
221  securityElement3.AddAttribute("Access", "Ping");
222  securityElement.AddChild(securityElement3);
223  }
224  return securityElement;
225  }
226  }
227 }
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...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
override IPermission Union(IPermission target)
Creates a permission that is the union of this permission and the specified permission.
string Tag
Gets or sets the tag name of an XML element.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
NetworkInformationPermission(NetworkInformationAccess access)
Initializes a new instance of the T:System.Net.NetworkInformation.NetworkInformationPermission class ...
override IPermission Copy()
Creates and returns an identical copy of this permission.
Represents the XML object model for encoding security objects. This class cannot be inherited.
NetworkInformationPermission(PermissionState state)
Initializes a new instance of the T:System.Net.NetworkInformation.NetworkInformationPermission class ...
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.
NetworkInformationAccess Access
Gets the level of access to network information controlled by this 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.
Attribute can be applied to a module.
NetworkInformationAccess
Specifies permission to access information about network interfaces and traffic statistics.
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.
override void FromXml(SecurityElement securityElement)
Sets the state of this permission using the specified XML encoding.
ArrayList Children
Gets or sets the array of child elements of the XML element.
bool IsUnrestricted()
Returns a value indicating whether the current permission is unrestricted.
override SecurityElement ToXml()
Creates an XML encoding of the state of this permission.
Controls access to network information and traffic statistics for the local computer....
void AddPermission(NetworkInformationAccess access)
Adds the specified value to this permission.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...