CredentialCache.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. {
  32. public class CredentialCache : ICredentials, IEnumerable
  33. {
  34. // Fields
  35. private Hashtable cache;
  36. // Constructors
  37. public CredentialCache ()
  38. {
  39. cache = new Hashtable ();
  40. }
  41. // Properties
  42. [MonoTODO ("Need EnvironmentPermission implementation first")]
  43. public static ICredentials DefaultCredentials {
  44. get {
  45. // This is used for NTLM, Kerberos and Negotiate under MS
  46. return null;
  47. }
  48. }
  49. // ICredentials
  50. public NetworkCredential GetCredential (Uri uriPrefix, string authType)
  51. {
  52. int longestPrefix = -1;
  53. NetworkCredential result = null;
  54. if (uriPrefix == null || authType == null)
  55. return null;
  56. string absPath = uriPrefix.AbsolutePath;
  57. absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  58. IDictionaryEnumerator e = cache.GetEnumerator ();
  59. while (e.MoveNext ()) {
  60. CredentialCacheKey key = e.Key as CredentialCacheKey;
  61. if (key.Length <= longestPrefix)
  62. continue;
  63. if (String.Compare (key.AuthType, authType, true) != 0)
  64. continue;
  65. Uri cachedUri = key.UriPrefix;
  66. if (cachedUri.Scheme != uriPrefix.Scheme)
  67. continue;
  68. if (cachedUri.Port != uriPrefix.Port)
  69. continue;
  70. if (cachedUri.Host != uriPrefix.Host)
  71. continue;
  72. if (!absPath.StartsWith (key.AbsPath))
  73. continue;
  74. longestPrefix = key.Length;
  75. result = (NetworkCredential) e.Value;
  76. }
  77. return result;
  78. }
  79. // IEnumerable
  80. public IEnumerator GetEnumerator ()
  81. {
  82. return cache.Values.GetEnumerator ();
  83. }
  84. // Methods
  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. // Inner Classes
  103. internal class CredentialCacheKey
  104. {
  105. private Uri uriPrefix;
  106. private string authType;
  107. private string absPath;
  108. private int len;
  109. private int hash;
  110. internal CredentialCacheKey (Uri uriPrefix, string authType)
  111. {
  112. this.uriPrefix = uriPrefix;
  113. this.authType = authType;
  114. this.absPath = uriPrefix.AbsolutePath;
  115. this.absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  116. this.len = uriPrefix.AbsoluteUri.Length;
  117. this.hash = uriPrefix.GetHashCode ()
  118. + authType.ToString ().GetHashCode ();
  119. }
  120. public int Length {
  121. get { return len; }
  122. }
  123. public string AbsPath {
  124. get { return absPath; }
  125. }
  126. public Uri UriPrefix {
  127. get { return uriPrefix; }
  128. }
  129. public string AuthType {
  130. get { return authType; }
  131. }
  132. public override int GetHashCode ()
  133. {
  134. return hash;
  135. }
  136. public override bool Equals (object obj)
  137. {
  138. CredentialCacheKey key = obj as CredentialCacheKey;
  139. return ((key != null) && (this.hash == key.hash));
  140. }
  141. public override string ToString ()
  142. {
  143. return absPath + " : " + authType + " : len=" + len;
  144. }
  145. }
  146. }
  147. }