mscorlib(4.0.0.0) API with additions
CodeConnectAccess.cs
3 
5 {
8  [ComVisible(true)]
9  public class CodeConnectAccess
10  {
11  private string _LowerCaseScheme;
12 
13  private string _LowerCasePort;
14 
15  private int _IntPort;
16 
17  private const string DefaultStr = "$default";
18 
19  private const string OriginStr = "$origin";
20 
21  internal const int NoPort = -1;
22 
23  internal const int AnyPort = -2;
24 
26  public static readonly int DefaultPort = -3;
27 
29  public static readonly int OriginPort = -4;
30 
32  public static readonly string OriginScheme = "$origin";
33 
35  public static readonly string AnyScheme = "*";
36 
39  public string Scheme => _LowerCaseScheme;
40 
43  public int Port => _IntPort;
44 
45  internal bool IsOriginScheme => (object)_LowerCaseScheme == OriginScheme;
46 
47  internal bool IsAnyScheme => (object)_LowerCaseScheme == AnyScheme;
48 
49  internal bool IsDefaultPort => Port == DefaultPort;
50 
51  internal bool IsOriginPort => Port == OriginPort;
52 
53  internal string StrPort => _LowerCasePort;
54 
64  public CodeConnectAccess(string allowScheme, int allowPort)
65  {
66  if (!IsValidScheme(allowScheme))
67  {
68  throw new ArgumentOutOfRangeException("allowScheme");
69  }
70  SetCodeConnectAccess(allowScheme.ToLower(CultureInfo.InvariantCulture), allowPort);
71  }
72 
79  public static CodeConnectAccess CreateOriginSchemeAccess(int allowPort)
80  {
81  CodeConnectAccess codeConnectAccess = new CodeConnectAccess();
82  codeConnectAccess.SetCodeConnectAccess(OriginScheme, allowPort);
83  return codeConnectAccess;
84  }
85 
92  public static CodeConnectAccess CreateAnySchemeAccess(int allowPort)
93  {
94  CodeConnectAccess codeConnectAccess = new CodeConnectAccess();
95  codeConnectAccess.SetCodeConnectAccess(AnyScheme, allowPort);
96  return codeConnectAccess;
97  }
98 
99  private CodeConnectAccess()
100  {
101  }
102 
103  private void SetCodeConnectAccess(string lowerCaseScheme, int allowPort)
104  {
105  _LowerCaseScheme = lowerCaseScheme;
106  if (allowPort == DefaultPort)
107  {
108  _LowerCasePort = "$default";
109  }
110  else if (allowPort == OriginPort)
111  {
112  _LowerCasePort = "$origin";
113  }
114  else
115  {
116  if (allowPort < 0 || allowPort > 65535)
117  {
118  throw new ArgumentOutOfRangeException("allowPort");
119  }
120  _LowerCasePort = allowPort.ToString(CultureInfo.InvariantCulture);
121  }
122  _IntPort = allowPort;
123  }
124 
129  public override bool Equals(object o)
130  {
131  if (this == o)
132  {
133  return true;
134  }
135  CodeConnectAccess codeConnectAccess = o as CodeConnectAccess;
136  if (codeConnectAccess == null)
137  {
138  return false;
139  }
140  if (Scheme == codeConnectAccess.Scheme)
141  {
142  return Port == codeConnectAccess.Port;
143  }
144  return false;
145  }
146 
149  public override int GetHashCode()
150  {
151  return Scheme.GetHashCode() + Port.GetHashCode();
152  }
153 
154  internal CodeConnectAccess(string allowScheme, string allowPort)
155  {
156  if (allowScheme == null || allowScheme.Length == 0)
157  {
158  throw new ArgumentNullException("allowScheme");
159  }
160  if (allowPort == null || allowPort.Length == 0)
161  {
162  throw new ArgumentNullException("allowPort");
163  }
164  _LowerCaseScheme = allowScheme.ToLower(CultureInfo.InvariantCulture);
165  if (_LowerCaseScheme == OriginScheme)
166  {
167  _LowerCaseScheme = OriginScheme;
168  }
169  else if (_LowerCaseScheme == AnyScheme)
170  {
171  _LowerCaseScheme = AnyScheme;
172  }
173  else if (!IsValidScheme(_LowerCaseScheme))
174  {
175  throw new ArgumentOutOfRangeException("allowScheme");
176  }
177  _LowerCasePort = allowPort.ToLower(CultureInfo.InvariantCulture);
178  if (_LowerCasePort == "$default")
179  {
180  _IntPort = DefaultPort;
181  return;
182  }
183  if (_LowerCasePort == "$origin")
184  {
185  _IntPort = OriginPort;
186  return;
187  }
188  _IntPort = int.Parse(allowPort, CultureInfo.InvariantCulture);
189  if (_IntPort < 0 || _IntPort > 65535)
190  {
191  throw new ArgumentOutOfRangeException("allowPort");
192  }
193  _LowerCasePort = _IntPort.ToString(CultureInfo.InvariantCulture);
194  }
195 
196  internal static bool IsValidScheme(string scheme)
197  {
198  if (scheme == null || scheme.Length == 0 || !IsAsciiLetter(scheme[0]))
199  {
200  return false;
201  }
202  for (int num = scheme.Length - 1; num > 0; num--)
203  {
204  if (!IsAsciiLetterOrDigit(scheme[num]) && scheme[num] != '+' && scheme[num] != '-' && scheme[num] != '.')
205  {
206  return false;
207  }
208  }
209  return true;
210  }
211 
212  private static bool IsAsciiLetterOrDigit(char character)
213  {
214  if (!IsAsciiLetter(character))
215  {
216  if (character >= '0')
217  {
218  return character <= '9';
219  }
220  return false;
221  }
222  return true;
223  }
224 
225  private static bool IsAsciiLetter(char character)
226  {
227  if (character < 'a' || character > 'z')
228  {
229  if (character >= 'A')
230  {
231  return character <= 'Z';
232  }
233  return false;
234  }
235  return true;
236  }
237  }
238 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
static readonly int OriginPort
Contains the value used to represent the port value in the URI where code originated.
override int GetHashCode()
Serves as a hash function for a particular type.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static readonly string OriginScheme
Contains the value used to represent the scheme in the URL where the code originated.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static readonly int DefaultPort
Contains the value used to represent the default port.
CodeConnectAccess(string allowScheme, int allowPort)
Initializes a new instance of the T:System.Security.Policy.CodeConnectAccess class.
static readonly string AnyScheme
Contains the string value that represents the scheme wildcard.
Specifies the network resource access that is granted to code.
static CodeConnectAccess CreateAnySchemeAccess(int allowPort)
Returns a T:System.Security.Policy.CodeConnectAccess instance that represents access to the specified...
override bool Equals(object o)
Returns a value indicating whether two T:System.Security.Policy.CodeConnectAccess objects represent t...
Specifies that the class can be serialized.
static CodeConnectAccess CreateOriginSchemeAccess(int allowPort)
Returns a T:System.Security.Policy.CodeConnectAccess instance that represents access to the specified...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
string Scheme
Gets the URI scheme represented by the current instance.
int Port
Gets the port represented by the current instance.