2 // System.Net.CredentialCache
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
9 using System.Collections;
\r
10 using System.Runtime.Serialization;
\r
12 namespace System.Net
\r
15 public class CredentialCache : ICredentials, IEnumerable
\r
18 private Hashtable cache;
\r
21 public CredentialCache ()
\r
23 cache = new Hashtable ();
\r
28 [MonoTODO ("Need EnvironmentPermission implementation first")]
\r
29 public static ICredentials DefaultCredentials {
\r
31 throw new NotImplementedException ();
\r
38 public NetworkCredential GetCredential (Uri uriPrefix, string authType)
\r
40 int longestPrefix = -1;
\r
41 NetworkCredential result = null;
\r
43 if (uriPrefix == null || authType == null)
\r
46 string absPath = uriPrefix.AbsolutePath;
\r
47 absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
\r
49 IDictionaryEnumerator e = cache.GetEnumerator ();
\r
50 while (e.MoveNext ()) {
\r
51 CredentialCacheKey key = e.Key as CredentialCacheKey;
\r
53 if (key.Length <= longestPrefix)
\r
56 if (String.Compare (key.AuthType, authType, true) != 0)
\r
59 Uri cachedUri = key.UriPrefix;
\r
61 if (cachedUri.Scheme != uriPrefix.Scheme)
\r
64 if (cachedUri.Port != uriPrefix.Port)
\r
67 if (cachedUri.Host != uriPrefix.Host)
\r
70 if (!absPath.StartsWith (key.AbsPath))
\r
73 longestPrefix = key.Length;
\r
74 result = (NetworkCredential) e.Value;
\r
82 public IEnumerator GetEnumerator ()
\r
84 return cache.Values.GetEnumerator ();
\r
89 public void Add (Uri uriPrefix, string authType, NetworkCredential cred)
\r
91 if (uriPrefix == null)
\r
92 throw new ArgumentNullException ("uriPrefix");
\r
94 if (authType == null)
\r
95 throw new ArgumentNullException ("authType");
\r
97 // throws ArgumentException when same key already exists.
\r
98 cache.Add (new CredentialCacheKey (uriPrefix, authType), cred);
\r
101 public void Remove (Uri uriPrefix, string authType)
\r
103 if (uriPrefix == null)
\r
104 throw new ArgumentNullException ("uriPrefix");
\r
106 if (authType == null)
\r
107 throw new ArgumentNullException ("authType");
\r
109 cache.Remove (new CredentialCacheKey (uriPrefix, authType));
\r
114 internal class CredentialCacheKey
\r
116 private Uri uriPrefix;
\r
117 private string authType;
\r
119 private string absPath;
\r
123 internal CredentialCacheKey (Uri uriPrefix, string authType)
\r
125 this.uriPrefix = uriPrefix;
\r
126 this.authType = authType;
\r
128 this.absPath = uriPrefix.AbsolutePath;
\r
129 this.absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));
\r
131 this.len = uriPrefix.AbsoluteUri.Length;
\r
132 this.hash = uriPrefix.GetHashCode ()
\r
133 + authType.ToString ().GetHashCode ();
\r
136 public int Length {
\r
137 get { return len; }
\r
140 public string AbsPath {
\r
141 get { return absPath; }
\r
144 public Uri UriPrefix {
\r
145 get { return uriPrefix; }
\r
148 public string AuthType {
\r
149 get { return authType; }
\r
152 public override int GetHashCode ()
\r
157 public override bool Equals (object obj)
\r
159 CredentialCacheKey key = obj as CredentialCacheKey;
\r
160 return ((key != null) && (this.hash == key.hash));
\r
163 public override string ToString ()
\r
165 return absPath + " : " + authType + " : len=" + len;
\r