2 // System.Net.CredentialCache.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Collections
;
31 using System
.Runtime
.Serialization
;
35 public class CredentialCache
: ICredentials
, IEnumerable
38 private Hashtable cache
;
41 public CredentialCache ()
43 cache
= new Hashtable ();
48 [MonoTODO ("Need EnvironmentPermission implementation first")]
49 public static ICredentials DefaultCredentials
{
51 // This is used for NTLM, Kerberos and Negotiate under MS
59 public NetworkCredential
GetCredential (Uri uriPrefix
, string authType
)
61 int longestPrefix
= -1;
62 NetworkCredential result
= null;
64 if (uriPrefix
== null || authType
== null)
67 string absPath
= uriPrefix
.AbsolutePath
;
68 absPath
= absPath
.Substring (0, absPath
.LastIndexOf ('/'));
70 IDictionaryEnumerator e
= cache
.GetEnumerator ();
71 while (e
.MoveNext ()) {
72 CredentialCacheKey key
= e
.Key
as CredentialCacheKey
;
74 if (key
.Length
<= longestPrefix
)
77 if (String
.Compare (key
.AuthType
, authType
, true) != 0)
80 Uri cachedUri
= key
.UriPrefix
;
82 if (cachedUri
.Scheme
!= uriPrefix
.Scheme
)
85 if (cachedUri
.Port
!= uriPrefix
.Port
)
88 if (cachedUri
.Host
!= uriPrefix
.Host
)
91 if (!absPath
.StartsWith (key
.AbsPath
))
94 longestPrefix
= key
.Length
;
95 result
= (NetworkCredential
) e
.Value
;
103 public IEnumerator
GetEnumerator ()
105 return cache
.Values
.GetEnumerator ();
110 public void Add (Uri uriPrefix
, string authType
, NetworkCredential cred
)
112 if (uriPrefix
== null)
113 throw new ArgumentNullException ("uriPrefix");
115 if (authType
== null)
116 throw new ArgumentNullException ("authType");
118 // throws ArgumentException when same key already exists.
119 cache
.Add (new CredentialCacheKey (uriPrefix
, authType
), cred
);
122 public void Remove (Uri uriPrefix
, string authType
)
124 if (uriPrefix
== null)
125 throw new ArgumentNullException ("uriPrefix");
127 if (authType
== null)
128 throw new ArgumentNullException ("authType");
130 cache
.Remove (new CredentialCacheKey (uriPrefix
, authType
));
135 internal class CredentialCacheKey
137 private Uri uriPrefix
;
138 private string authType
;
140 private string absPath
;
144 internal CredentialCacheKey (Uri uriPrefix
, string authType
)
146 this.uriPrefix
= uriPrefix
;
147 this.authType
= authType
;
149 this.absPath
= uriPrefix
.AbsolutePath
;
150 this.absPath
= absPath
.Substring (0, absPath
.LastIndexOf ('/'));
152 this.len
= uriPrefix
.AbsoluteUri
.Length
;
153 this.hash
= uriPrefix
.GetHashCode ()
154 + authType
.ToString ().GetHashCode ();
161 public string AbsPath
{
162 get { return absPath; }
165 public Uri UriPrefix
{
166 get { return uriPrefix; }
169 public string AuthType
{
170 get { return authType; }
173 public override int GetHashCode ()
178 public override bool Equals (object obj
)
180 CredentialCacheKey key
= obj
as CredentialCacheKey
;
181 return ((key
!= null) && (this.hash
== key
.hash
));
184 public override string ToString ()
186 return absPath
+ " : " + authType
+ " : len=" + len
;