static void CheckExpired(int count) { if (count < 10) { return; } DateTime t = DateTime.MaxValue; DateTime now = DateTime.Now; List <int> list = null; foreach (KeyValuePair <int, AuthDigestSession> kvp in cache) { AuthDigestSession elem = kvp.Value; if (elem.LastUse < t && (elem.LastUse - now).Ticks > TimeSpan.TicksPerMinute * 10) { t = elem.LastUse; if (list == null) { list = new List <int> (); } list.Add(kvp.Key); } } if (list != null) { foreach (int k in list) { cache.Remove(k); } } }
public Authorization Authenticate(string challenge, HttpURLConnection request, ICredentials credentials) { if (credentials == null || challenge == null) { return(null); } string header = challenge.Trim(); if (header.IndexOf("digest", StringComparison.OrdinalIgnoreCase) == -1) { return(null); } var currDS = new AuthDigestSession(); if (!currDS.Parse(challenge)) { return(null); } var uri = new Uri(request.URL.ToString()); int hashcode = uri.GetHashCode() ^ credentials.GetHashCode() ^ currDS.Nonce.GetHashCode(); AuthDigestSession ds = null; bool addDS = false; if (!Cache.TryGetValue(hashcode, out ds) || ds == null) { addDS = true; } if (addDS) { ds = currDS; } else if (!ds.Parse(challenge)) { return(null); } if (addDS) { Cache.Add(hashcode, ds); } return(ds.Authenticate(request, credentials)); }
public Authorization PreAuthenticate(HttpURLConnection request, ICredentials credentials) { if (request == null || credentials == null) { return(null); } var uri = new Uri(request.URL.ToString()); int hashcode = uri.GetHashCode() ^ credentials.GetHashCode(); AuthDigestSession ds = null; if (!Cache.TryGetValue(hashcode, out ds) || ds == null) { return(null); } return(ds.Authenticate(request, credentials)); }