/// <summary>Returns the <see cref="T:System.Net.NetworkCredential" /> instance associated with the specified Uniform Resource Identifier (URI) and authentication type.</summary> /// <returns>A <see cref="T:System.Net.NetworkCredential" /> or, if there is no matching credential in the cache, null.</returns> /// <param name="uriPrefix">A <see cref="T:System.Uri" /> that specifies the URI prefix of the resources that the credential grants access to. </param> /// <param name="authType">The authentication scheme used by the resource named in <paramref name="uriPrefix" />. </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="uriPrefix" /> or <paramref name="authType" /> is null. </exception> public NetworkCredential GetCredential(Uri uriPrefix, string authType) { int num = -1; NetworkCredential result = null; if (uriPrefix == null || authType == null) { return(null); } string absolutePath = uriPrefix.AbsolutePath; absolutePath = absolutePath.Substring(0, absolutePath.LastIndexOf('/')); IDictionaryEnumerator enumerator = cache.GetEnumerator(); while (enumerator.MoveNext()) { CredentialCacheKey credentialCacheKey = enumerator.Key as CredentialCacheKey; if (credentialCacheKey.Length > num && string.Compare(credentialCacheKey.AuthType, authType, ignoreCase: true) == 0) { Uri uriPrefix2 = credentialCacheKey.UriPrefix; if (!(uriPrefix2.Scheme != uriPrefix.Scheme) && uriPrefix2.Port == uriPrefix.Port && !(uriPrefix2.Host != uriPrefix.Host) && absolutePath.StartsWith(credentialCacheKey.AbsPath)) { num = credentialCacheKey.Length; result = (NetworkCredential)enumerator.Value; } } } return(result); }
public NetworkCredential GetCredential(Uri uriPrefix, string authType) { int longestPrefix = -1; NetworkCredential result = null; if (uriPrefix == null || authType == null) { return(null); } string absPath = uriPrefix.AbsolutePath; absPath = absPath.Substring(0, absPath.LastIndexOf('/')); IDictionaryEnumerator e = cache.GetEnumerator(); while (e.MoveNext()) { CredentialCacheKey key = e.Key as CredentialCacheKey; if (key.Length <= longestPrefix) { continue; } if (String.Compare(key.AuthType, authType, true) != 0) { continue; } Uri cachedUri = key.UriPrefix; if (cachedUri.Scheme != uriPrefix.Scheme) { continue; } if (cachedUri.Port != uriPrefix.Port) { continue; } if (cachedUri.Host != uriPrefix.Host) { continue; } if (!absPath.StartsWith(key.AbsPath)) { continue; } longestPrefix = key.Length; result = (NetworkCredential)e.Value; } return(result); }
public void VerifyBlobSerialization() { var accountSessions = new List <AccountSession> { new AccountSession { AccessToken = "token", ClientId = "1", ExpiresOnUtc = DateTimeOffset.Now, RefreshToken = "refresh", Scopes = new string[] { "scope1", "scope2" }, UserId = "1", }, new AccountSession { AccessToken = "token2", ClientId = "2", ExpiresOnUtc = DateTimeOffset.Now, RefreshToken = "refresh2", Scopes = new string[] { "scope" }, UserId = "2", } }; foreach (var accountSession in accountSessions) { this.credentialCache.AddToCache(accountSession); } var cacheBlob = this.credentialCache.GetCacheBlob(); var newCredentialCache = new CredentialCache(cacheBlob); Assert.AreEqual(2, newCredentialCache.cacheDictionary.Count, "Unexpected number of cache entries."); foreach (var accountSession in accountSessions) { var accountSessionKey = new CredentialCacheKey { ClientId = accountSession.ClientId, UserId = accountSession.UserId, }; var sessionFromCacheDictionary = newCredentialCache.cacheDictionary[accountSessionKey]; Assert.IsNotNull(sessionFromCacheDictionary, "Unexpected account session returned."); Assert.AreEqual(accountSession.AccessToken, sessionFromCacheDictionary.AccessToken, "Unexpected access token returned."); Assert.AreEqual(accountSession.ClientId, sessionFromCacheDictionary.ClientId, "Unexpected client ID returned."); Assert.AreEqual(accountSession.ExpiresOnUtc, sessionFromCacheDictionary.ExpiresOnUtc, "Unexpected expiration returned."); Assert.AreEqual(accountSession.RefreshToken, sessionFromCacheDictionary.RefreshToken, "Unexpected refresh token returned."); Assert.AreEqual(accountSession.UserId, sessionFromCacheDictionary.UserId, "Unexpected access token returned."); Assert.AreEqual(accountSession.Scopes.Length, sessionFromCacheDictionary.Scopes.Length, "Unexpected scopes returned."); for (int i = 0; i < accountSession.Scopes.Length; i++) { Assert.AreEqual(accountSession.Scopes[i], sessionFromCacheDictionary.Scopes[i], "Unexpected scope returned."); } } }
public void VerifyCacheKeyComparison_NotEqual() { var cacheKeyLower = new CredentialCacheKey { UserId = "abc", }; var cacheKeyUpper = new CredentialCacheKey { ClientId = "CLIENTID", UserId = "ABC", }; Assert.AreNotEqual(cacheKeyLower, cacheKeyUpper, "Cache key comparison failed."); }
/// <summary> /// Attempt to re-query the credentials for the specified URI /// </summary> /// <param name="entryUri">The entry URI to update credentials for</param> /// <param name="repositoryId">The owner Id to specify to the server (for example repository Id)</param> /// <param name="forceUpdate">True to force a requery to the user even if they previously canceled requesting credentials</param> /// <returns>True if the user provided updated credentials, false if they canceled</returns> public static bool UpdateCredentials(string entryUri, Guid repositoryId, bool forceUpdate) { var cacheKey = new CredentialCacheKey(entryUri, repositoryId); bool newCredentialsProvided = false; lock (g_Lock) //if we have any in cache we need to update those, so in this case we stall EVERYONE. { //WAIT: Since we requested the lock someone may have put credentials in the collection, so we have to check again. System.Threading.Monitor.PulseAll(g_Lock); if ((forceUpdate == false) && (g_CachedBlockedCredentials.ContainsKey(entryUri))) { return(false); } IWebAuthenticationProvider credentials; g_CachedCredentials.TryGetValue(cacheKey, out credentials); //we only want one thread to pop up the UI to request authentication at a time. lock (g_RequestLock) { var credentialEventArgs = new CredentialsRequiredEventArgs(entryUri, repositoryId, true, credentials); OnCredentialsRequired(null, credentialEventArgs); if (credentialEventArgs.Cancel == false) { if (credentialEventArgs.AuthenticationProvider == null) { throw new InvalidOperationException("No credentials are available for the specified server"); } newCredentialsProvided = true; g_CachedBlockedCredentials.Remove(entryUri); //if it was previously blocked, unblock it. g_CachedCredentials[cacheKey] = credentialEventArgs.AuthenticationProvider; //overwrite any existing value. } System.Threading.Monitor.PulseAll(g_RequestLock); } System.Threading.Monitor.PulseAll(g_Lock); } return(newCredentialsProvided); }
public void VerifyCacheKeyComparison_Equal() { var cacheKeyLower = new CredentialCacheKey { AccountType = AccountType.MicrosoftAccount, ClientId = "clientid", UserId = "abc", }; var cacheKeyUpper = new CredentialCacheKey { AccountType = AccountType.MicrosoftAccount, ClientId = "CLIENTID", UserId = "ABC", }; Assert.AreEqual(cacheKeyLower, cacheKeyUpper, "Cache key comparison failed."); }
public override bool Equals(object obj) { CredentialCacheKey key = obj as CredentialCacheKey; return((key != null) && (this.hash == key.hash)); }
public override bool Equals(object obj) { CredentialCacheKey credentialCacheKey = obj as CredentialCacheKey; return(credentialCacheKey != null && hash == credentialCacheKey.hash); }