System.Net.CredentialCache.GetCredential C# (CSharp) Method

GetCredential() public method

public GetCredential ( Uri uriPrefix, string authType ) : NetworkCredential
uriPrefix Uri
authType string
return NetworkCredential
        public NetworkCredential GetCredential(Uri uriPrefix, string authType) {
            if (uriPrefix==null)
                throw new ArgumentNullException("uriPrefix");
            if (authType==null)
                throw new ArgumentNullException("authType");
            
            GlobalLog.Print("CredentialCache::GetCredential(uriPrefix=\"" + uriPrefix + "\", authType=\"" + authType + "\")");

            int longestMatchPrefix = -1;
            NetworkCredential mostSpecificMatch = null;
            IDictionaryEnumerator credEnum = cache.GetEnumerator();

            //
            // Enumerate through every credential in the cache
            //

            while (credEnum.MoveNext()) {

                CredentialKey key = (CredentialKey)credEnum.Key;

                //
                // Determine if this credential is applicable to the current Uri/AuthType
                //

                if (key.Match(uriPrefix, authType)) {

                    int prefixLen = key.UriPrefixLength;

                    //
                    // Check if the match is better than the current-most-specific match
                    //

                    if (prefixLen > longestMatchPrefix) {

                        //
                        // Yes-- update the information about currently preferred match
                        //

                        longestMatchPrefix = prefixLen;
                        mostSpecificMatch = (NetworkCredential)credEnum.Value;
                    }
                }
            }

            GlobalLog.Print("CredentialCache::GetCredential returning " + ((mostSpecificMatch==null)?"null":"(" + mostSpecificMatch.UserName + ":" + mostSpecificMatch.Domain + ")"));

            return mostSpecificMatch;
        }

Same methods

CredentialCache::GetCredential ( string host, int port, string authenticationType ) : NetworkCredential

Usage Example

Example #1
0
		private void SendReport()
		{
			try
			{
				var fileName = Path.GetTempFileName();
				File.WriteAllText(fileName, report, Encoding.Unicode);

				CredentialCache credentialCache = new CredentialCache();
				credentialCache.Add(uploadUri, @"Basic", new NetworkCredential(@"uprep", @"qeiusroi123woi3zf"));

				var webClient = new WebClient();
				webClient.Credentials = credentialCache.GetCredential(uploadUri, @"Basic");
				webClient.QueryString.Add("app", "SRV");
				webClient.QueryString.Add("ver", version.ToString());
				webClient.UploadFile(uploadUri, fileName);
			}
			catch
			{
			}
		}
All Usage Examples Of System.Net.CredentialCache::GetCredential