mscorlib(4.0.0.0) API with additions
CredentialCache.cs
1 using System.Collections;
3 
4 namespace System.Net
5 {
7  [global::__DynamicallyInvokable]
9  {
10  private class CredentialEnumerator : IEnumerator
11  {
12  private CredentialCache m_cache;
13 
14  private ICredentials[] m_array;
15 
16  private int m_index = -1;
17 
18  private int m_version;
19 
20  object IEnumerator.Current
21  {
22  get
23  {
24  if (m_index < 0 || m_index >= m_array.Length)
25  {
26  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumOpCantHappen"));
27  }
28  if (m_version != m_cache.m_version)
29  {
30  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumFailedVersion"));
31  }
32  return m_array[m_index];
33  }
34  }
35 
36  internal CredentialEnumerator(CredentialCache cache, Hashtable table, Hashtable hostTable, int version)
37  {
38  m_cache = cache;
39  m_array = new ICredentials[table.Count + hostTable.Count];
40  table.Values.CopyTo(m_array, 0);
41  hostTable.Values.CopyTo(m_array, table.Count);
42  m_version = version;
43  }
44 
45  bool IEnumerator.MoveNext()
46  {
47  if (m_version != m_cache.m_version)
48  {
49  throw new InvalidOperationException(SR.GetString("InvalidOperation_EnumFailedVersion"));
50  }
51  if (++m_index < m_array.Length)
52  {
53  return true;
54  }
55  m_index = m_array.Length;
56  return false;
57  }
58 
59  void IEnumerator.Reset()
60  {
61  m_index = -1;
62  }
63  }
64 
65  private Hashtable cache = new Hashtable();
66 
67  private Hashtable cacheForHosts = new Hashtable();
68 
69  internal int m_version;
70 
71  private int m_NumbDefaultCredInCache;
72 
73  internal bool IsDefaultInCache => m_NumbDefaultCredInCache != 0;
74 
77  [global::__DynamicallyInvokable]
78  public static ICredentials DefaultCredentials
79  {
80  [global::__DynamicallyInvokable]
81  get
82  {
84  return SystemNetworkCredential.defaultCredential;
85  }
86  }
87 
90  [global::__DynamicallyInvokable]
92  {
93  [global::__DynamicallyInvokable]
94  get
95  {
97  return SystemNetworkCredential.defaultCredential;
98  }
99  }
100 
102  [global::__DynamicallyInvokable]
104  {
105  }
106 
115  [global::__DynamicallyInvokable]
116  public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
117  {
118  if (uriPrefix == null)
119  {
120  throw new ArgumentNullException("uriPrefix");
121  }
122  if (authType == null)
123  {
124  throw new ArgumentNullException("authType");
125  }
126  if (cred is SystemNetworkCredential && string.Compare(authType, "NTLM", StringComparison.OrdinalIgnoreCase) != 0 && (!DigestClient.WDigestAvailable || string.Compare(authType, "Digest", StringComparison.OrdinalIgnoreCase) != 0) && string.Compare(authType, "Kerberos", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(authType, "Negotiate", StringComparison.OrdinalIgnoreCase) != 0)
127  {
128  throw new ArgumentException(SR.GetString("net_nodefaultcreds", authType), "authType");
129  }
130  m_version++;
131  CredentialKey key = new CredentialKey(uriPrefix, authType);
132  cache.Add(key, cred);
133  if (cred is SystemNetworkCredential)
134  {
135  m_NumbDefaultCredInCache++;
136  }
137  }
138 
151  [global::__DynamicallyInvokable]
152  public void Add(string host, int port, string authenticationType, NetworkCredential credential)
153  {
154  if (host == null)
155  {
156  throw new ArgumentNullException("host");
157  }
158  if (authenticationType == null)
159  {
160  throw new ArgumentNullException("authenticationType");
161  }
162  if (host.Length == 0)
163  {
164  throw new ArgumentException(SR.GetString("net_emptystringcall", "host"));
165  }
166  if (port < 0)
167  {
168  throw new ArgumentOutOfRangeException("port");
169  }
170  if (credential is SystemNetworkCredential && string.Compare(authenticationType, "NTLM", StringComparison.OrdinalIgnoreCase) != 0 && (!DigestClient.WDigestAvailable || string.Compare(authenticationType, "Digest", StringComparison.OrdinalIgnoreCase) != 0) && string.Compare(authenticationType, "Kerberos", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(authenticationType, "Negotiate", StringComparison.OrdinalIgnoreCase) != 0)
171  {
172  throw new ArgumentException(SR.GetString("net_nodefaultcreds", authenticationType), "authenticationType");
173  }
174  m_version++;
175  CredentialHostKey key = new CredentialHostKey(host, port, authenticationType);
176  cacheForHosts.Add(key, credential);
177  if (credential is SystemNetworkCredential)
178  {
179  m_NumbDefaultCredInCache++;
180  }
181  }
182 
186  [global::__DynamicallyInvokable]
187  public void Remove(Uri uriPrefix, string authType)
188  {
189  if (!(uriPrefix == null) && authType != null)
190  {
191  m_version++;
192  CredentialKey key = new CredentialKey(uriPrefix, authType);
193  if (cache[key] is SystemNetworkCredential)
194  {
195  m_NumbDefaultCredInCache--;
196  }
197  cache.Remove(key);
198  }
199  }
200 
205  [global::__DynamicallyInvokable]
206  public void Remove(string host, int port, string authenticationType)
207  {
208  if (host != null && authenticationType != null && port >= 0)
209  {
210  m_version++;
211  CredentialHostKey key = new CredentialHostKey(host, port, authenticationType);
212  if (cacheForHosts[key] is SystemNetworkCredential)
213  {
214  m_NumbDefaultCredInCache--;
215  }
216  cacheForHosts.Remove(key);
217  }
218  }
219 
226  [global::__DynamicallyInvokable]
227  public NetworkCredential GetCredential(Uri uriPrefix, string authType)
228  {
229  if (uriPrefix == null)
230  {
231  throw new ArgumentNullException("uriPrefix");
232  }
233  if (authType == null)
234  {
235  throw new ArgumentNullException("authType");
236  }
237  int num = -1;
238  NetworkCredential result = null;
239  IDictionaryEnumerator enumerator = cache.GetEnumerator();
240  while (enumerator.MoveNext())
241  {
242  CredentialKey credentialKey = (CredentialKey)enumerator.Key;
243  if (credentialKey.Match(uriPrefix, authType))
244  {
245  int uriPrefixLength = credentialKey.UriPrefixLength;
246  if (uriPrefixLength > num)
247  {
248  num = uriPrefixLength;
249  result = (NetworkCredential)enumerator.Value;
250  }
251  }
252  }
253  return result;
254  }
255 
269  [global::__DynamicallyInvokable]
270  public NetworkCredential GetCredential(string host, int port, string authenticationType)
271  {
272  if (host == null)
273  {
274  throw new ArgumentNullException("host");
275  }
276  if (authenticationType == null)
277  {
278  throw new ArgumentNullException("authenticationType");
279  }
280  if (host.Length == 0)
281  {
282  throw new ArgumentException(SR.GetString("net_emptystringcall", "host"));
283  }
284  if (port < 0)
285  {
286  throw new ArgumentOutOfRangeException("port");
287  }
288  NetworkCredential result = null;
289  IDictionaryEnumerator enumerator = cacheForHosts.GetEnumerator();
290  while (enumerator.MoveNext())
291  {
292  CredentialHostKey credentialHostKey = (CredentialHostKey)enumerator.Key;
293  if (credentialHostKey.Match(host, port, authenticationType))
294  {
295  result = (NetworkCredential)enumerator.Value;
296  }
297  }
298  return result;
299  }
300 
303  [global::__DynamicallyInvokable]
305  {
306  return new CredentialEnumerator(this, cache, cacheForHosts, m_version);
307  }
308  }
309 }
IEnumerator GetEnumerator()
Returns an enumerator that can iterate through the T:System.Net.CredentialCache instance.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Remove(string host, int port, string authenticationType)
Deletes a T:System.Net.NetworkCredential instance from the cache if it is associated with the specifi...
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
bool MoveNext()
Advances the enumerator to the next element of the collection.
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.
void Remove(Uri uriPrefix, string authType)
Deletes a T:System.Net.NetworkCredential instance from the cache if it is associated with the specifi...
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
EnvironmentPermissionAccess
Specifies access to environment variables.
virtual ICollection Values
Gets an T:System.Collections.ICollection containing the values in the T:System.Collections....
Definition: Hashtable.cs:631
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
void Add(string host, int port, string authenticationType, NetworkCredential credential)
Adds a T:System.Net.NetworkCredential instance for use with SMTP to the credential cache and associat...
NetworkCredential GetCredential(Uri uriPrefix, string authType)
Returns the T:System.Net.NetworkCredential instance associated with the specified Uniform Resource Id...
Provides storage for multiple credentials.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
void Add(Uri uriPrefix, string authType, NetworkCredential cred)
Adds a T:System.Net.NetworkCredential instance to the credential cache for use with protocols other t...
static ICredentials DefaultCredentials
Gets the system credentials of the application.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Provides the interface for retrieving credentials for a host, port, and authentication type.
Provides the base authentication interface for retrieving credentials for Web client authentication.
Definition: ICredentials.cs:5
NetworkCredential GetCredential(string host, int port, string authenticationType)
Returns the T:System.Net.NetworkCredential instance associated with the specified host,...
CredentialCache()
Creates a new instance of the T:System.Net.CredentialCache class.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static NetworkCredential DefaultNetworkCredentials
Gets the network credentials of the current security context.
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
object Value
Gets the value of the current dictionary entry.
Enumerates the elements of a nongeneric dictionary.
The exception that is thrown when a method call is invalid for the object's current state.
Controls access to system and user environment variables. This class cannot be inherited.
Provides credentials for password-based authentication schemes such as basic, digest,...
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658