CredentialCache.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.Net.CredentialCache
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Runtime.Serialization;
  10. namespace System.Net
  11. {
  12. [Serializable]
  13. public class CredentialCache : ICredentials, IEnumerable
  14. {
  15. // Fields
  16. private Hashtable cache;
  17. // Constructors
  18. public CredentialCache ()
  19. {
  20. cache = new Hashtable ();
  21. }
  22. // Properties
  23. [MonoTODO ("Need EnvironmentPermission implementation first")]
  24. public static ICredentials DefaultCredentials {
  25. get {
  26. throw new NotImplementedException ();
  27. }
  28. }
  29. // ICredentials
  30. public NetworkCredential GetCredential (Uri uriPrefix, string authType)
  31. {
  32. int longestPrefix = -1;
  33. NetworkCredential result = null;
  34. if (uriPrefix == null || authType == null)
  35. return null;
  36. string absPath = uriPrefix.AbsolutePath;
  37. absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  38. IDictionaryEnumerator e = cache.GetEnumerator ();
  39. while (e.MoveNext ()) {
  40. CredentialCacheKey key = e.Key as CredentialCacheKey;
  41. if (key.Length <= longestPrefix)
  42. continue;
  43. if (String.Compare (key.AuthType, authType, true) != 0)
  44. continue;
  45. Uri cachedUri = key.UriPrefix;
  46. if (cachedUri.Scheme != uriPrefix.Scheme)
  47. continue;
  48. if (cachedUri.Port != uriPrefix.Port)
  49. continue;
  50. if (cachedUri.Host != uriPrefix.Host)
  51. continue;
  52. if (!absPath.StartsWith (key.AbsPath))
  53. continue;
  54. longestPrefix = key.Length;
  55. result = (NetworkCredential) e.Value;
  56. }
  57. return result;
  58. }
  59. // IEnumerable
  60. public IEnumerator GetEnumerator ()
  61. {
  62. return cache.Values.GetEnumerator ();
  63. }
  64. // Methods
  65. public void Add (Uri uriPrefix, string authType, NetworkCredential cred)
  66. {
  67. if (uriPrefix == null)
  68. throw new ArgumentNullException ("uriPrefix");
  69. if (authType == null)
  70. throw new ArgumentNullException ("authType");
  71. // throws ArgumentException when same key already exists.
  72. cache.Add (new CredentialCacheKey (uriPrefix, authType), cred);
  73. }
  74. public void Remove (Uri uriPrefix, string authType)
  75. {
  76. if (uriPrefix == null)
  77. throw new ArgumentNullException ("uriPrefix");
  78. if (authType == null)
  79. throw new ArgumentNullException ("authType");
  80. cache.Remove (new CredentialCacheKey (uriPrefix, authType));
  81. }
  82. // Inner Classes
  83. internal class CredentialCacheKey
  84. {
  85. private Uri uriPrefix;
  86. private string authType;
  87. private string absPath;
  88. private int len;
  89. private int hash;
  90. internal CredentialCacheKey (Uri uriPrefix, string authType)
  91. {
  92. this.uriPrefix = uriPrefix;
  93. this.authType = authType;
  94. this.absPath = uriPrefix.AbsolutePath;
  95. this.absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
  96. this.len = uriPrefix.AbsoluteUri.Length;
  97. this.hash = uriPrefix.GetHashCode ()
  98. + authType.ToString ().GetHashCode ();
  99. }
  100. public int Length {
  101. get { return len; }
  102. }
  103. public string AbsPath {
  104. get { return absPath; }
  105. }
  106. public Uri UriPrefix {
  107. get { return uriPrefix; }
  108. }
  109. public string AuthType {
  110. get { return authType; }
  111. }
  112. public override int GetHashCode ()
  113. {
  114. return hash;
  115. }
  116. public override bool Equals (object obj)
  117. {
  118. CredentialCacheKey key = obj as CredentialCacheKey;
  119. return ((key != null) && (this.hash == key.hash));
  120. }
  121. public override string ToString ()
  122. {
  123. return absPath + " : " + authType + " : len=" + len;
  124. }
  125. }
  126. }
  127. }