CredentialCache.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Net.CredentialCache.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Runtime.Serialization;
  30. namespace System.Net {
  31. public class CredentialCache : ICredentials, IEnumerable {
  32. static NetworkCredential empty = new NetworkCredential ("", "", "");
  33. Hashtable cache;
  34. public CredentialCache ()
  35. {
  36. cache = new Hashtable ();
  37. }
  38. [MonoTODO ("Need EnvironmentPermission implementation first")]
  39. public static ICredentials DefaultCredentials {
  40. get {
  41. // This is used for NTLM, Kerberos and Negotiate under MS
  42. return empty;
  43. }
  44. }
  45. #if NET_2_0
  46. // MS does might return a special ICredentials which does not allow getting the
  47. // username/password information out of it for non-internal classes.
  48. public static NetworkCredential DefaultNetworkCredentials {
  49. get { return empty; }
  50. }
  51. #endif
  52. public NetworkCredential GetCredential (Uri uriPrefix, string authType)
  53. {
  54. int longestPrefix = -1;
  55. NetworkCredential result = null;
  56. if (uriPrefix == null || authType == null)
  57. return null;
  58. string absPath = uriPrefix.AbsolutePath;
  59. absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  60. IDictionaryEnumerator e = cache.GetEnumerator ();
  61. while (e.MoveNext ()) {
  62. CredentialCacheKey key = e.Key as CredentialCacheKey;
  63. if (key.Length <= longestPrefix)
  64. continue;
  65. if (String.Compare (key.AuthType, authType, true) != 0)
  66. continue;
  67. Uri cachedUri = key.UriPrefix;
  68. if (cachedUri.Scheme != uriPrefix.Scheme)
  69. continue;
  70. if (cachedUri.Port != uriPrefix.Port)
  71. continue;
  72. if (cachedUri.Host != uriPrefix.Host)
  73. continue;
  74. if (!absPath.StartsWith (key.AbsPath))
  75. continue;
  76. longestPrefix = key.Length;
  77. result = (NetworkCredential) e.Value;
  78. }
  79. return result;
  80. }
  81. public IEnumerator GetEnumerator ()
  82. {
  83. return cache.Values.GetEnumerator ();
  84. }
  85. public void Add (Uri uriPrefix, string authType, NetworkCredential cred)
  86. {
  87. if (uriPrefix == null)
  88. throw new ArgumentNullException ("uriPrefix");
  89. if (authType == null)
  90. throw new ArgumentNullException ("authType");
  91. // throws ArgumentException when same key already exists.
  92. cache.Add (new CredentialCacheKey (uriPrefix, authType), cred);
  93. }
  94. public void Remove (Uri uriPrefix, string authType)
  95. {
  96. if (uriPrefix == null)
  97. throw new ArgumentNullException ("uriPrefix");
  98. if (authType == null)
  99. throw new ArgumentNullException ("authType");
  100. cache.Remove (new CredentialCacheKey (uriPrefix, authType));
  101. }
  102. #if NET_2_0
  103. [MonoNotSupported ("")]
  104. public void Add (string host, int port, string authenticationType, NetworkCredential credential)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. [MonoNotSupported ("")]
  109. public NetworkCredential GetCredential (string host, int port, string authenticationType)
  110. {
  111. throw new NotImplementedException ();
  112. }
  113. [MonoNotSupported ("")]
  114. public void Remove (string host, int port, string authenticationType)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. #endif
  119. class CredentialCacheKey {
  120. Uri uriPrefix;
  121. string authType;
  122. string absPath;
  123. int len;
  124. int hash;
  125. internal CredentialCacheKey (Uri uriPrefix, string authType)
  126. {
  127. this.uriPrefix = uriPrefix;
  128. this.authType = authType;
  129. this.absPath = uriPrefix.AbsolutePath;
  130. this.absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  131. this.len = uriPrefix.AbsoluteUri.Length;
  132. this.hash = uriPrefix.GetHashCode ()
  133. + authType.ToString ().GetHashCode ();
  134. }
  135. public int Length {
  136. get { return len; }
  137. }
  138. public string AbsPath {
  139. get { return absPath; }
  140. }
  141. public Uri UriPrefix {
  142. get { return uriPrefix; }
  143. }
  144. public string AuthType {
  145. get { return authType; }
  146. }
  147. public override int GetHashCode ()
  148. {
  149. return hash;
  150. }
  151. public override bool Equals (object obj)
  152. {
  153. CredentialCacheKey key = obj as CredentialCacheKey;
  154. return ((key != null) && (this.hash == key.hash));
  155. }
  156. public override string ToString ()
  157. {
  158. return absPath + " : " + authType + " : len=" + len;
  159. }
  160. }
  161. }
  162. }