using System.Runtime.Serialization;
namespace System.Net {
- public class CredentialCache : ICredentials, IEnumerable
-#if NET_2_0
- , ICredentialsByHost
-#endif
+ public class CredentialCache : ICredentials, IEnumerable, ICredentialsByHost
{
static NetworkCredential empty = new NetworkCredential (String.Empty, String.Empty, String.Empty);
Hashtable cache;
+ Hashtable cacheForHost;
public CredentialCache ()
{
cache = new Hashtable ();
+ cacheForHost = new Hashtable ();
}
[MonoTODO ("Need EnvironmentPermission implementation first")]
}
}
-#if NET_2_0
// MS does might return a special ICredentials which does not allow getting the
// username/password information out of it for non-internal classes.
public static NetworkCredential DefaultNetworkCredentials {
get { return empty; }
}
-#endif
public NetworkCredential GetCredential (Uri uriPrefix, string authType)
{
cache.Remove (new CredentialCacheKey (uriPrefix, authType));
}
-#if NET_2_0
- [MonoNotSupported ("")]
- public void Add (string host, int port, string authenticationType, NetworkCredential credential)
+ public NetworkCredential GetCredential (string host, int port, string authenticationType)
{
- throw new NotImplementedException ();
+ NetworkCredential result = null;
+
+ if (host == null || port < 0 || authenticationType == null)
+ return null;
+
+ IDictionaryEnumerator e = cacheForHost.GetEnumerator ();
+ while (e.MoveNext ()) {
+ CredentialCacheForHostKey key = e.Key as CredentialCacheForHostKey;
+
+ if (String.Compare (key.AuthType, authenticationType, true) != 0)
+ continue;
+
+ if (key.Host != host)
+ continue;
+
+ if (key.Port != port)
+ continue;
+
+ result = (NetworkCredential) e.Value;
+ }
+
+ return result;
}
- [MonoNotSupported ("")]
- public NetworkCredential GetCredential (string host, int port, string authenticationType)
+ public void Add (string host, int port, string authenticationType, NetworkCredential credential)
{
- throw new NotImplementedException ();
+ if (host == null)
+ throw new ArgumentNullException("host");
+
+ if (port < 0)
+ throw new ArgumentOutOfRangeException("port");
+
+ if (authenticationType == null)
+ throw new ArgumentOutOfRangeException("authenticationType");
+
+ cacheForHost.Add (new CredentialCacheForHostKey (host, port, authenticationType), credential);
}
- [MonoNotSupported ("")]
public void Remove (string host, int port, string authenticationType)
{
- throw new NotImplementedException ();
+ if (host == null)
+ return;
+
+ if (authenticationType == null)
+ return;
+
+ cacheForHost.Remove (new CredentialCacheForHostKey (host, port, authenticationType));
}
-#endif
class CredentialCacheKey {
Uri uriPrefix;
return absPath + " : " + authType + " : len=" + len;
}
}
- }
+
+ class CredentialCacheForHostKey {
+ string host;
+ int port;
+ string authType;
+ int hash;
+
+ internal CredentialCacheForHostKey (string host, int port, string authType)
+ {
+ this.host = host;
+ this.port = port;
+ this.authType = authType;
+
+ this.hash = host.GetHashCode ()
+ + port.GetHashCode ()
+ + authType.GetHashCode ();
+ }
+
+ public string Host {
+ get { return host; }
+ }
+
+ public int Port {
+ get { return port; }
+ }
+
+ public string AuthType {
+ get { return authType; }
+ }
+
+ public override int GetHashCode ()
+ {
+ return hash;
+ }
+
+ public override bool Equals (object obj)
+ {
+ CredentialCacheForHostKey key = obj as CredentialCacheForHostKey;
+ return ((key != null) && (this.hash == key.hash));
+ }
+
+ public override string ToString ()
+ {
+ return host + " : " + authType;
+ }
+ }
+}
}