CredentialCache.cs 3.7 KB

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