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

GetCredential() public method

public GetCredential ( string host, int port, string authenticationType ) : NetworkCredential
host string
port int
authenticationType string
return NetworkCredential
        public NetworkCredential GetCredential(string host, int port, string authenticationType) {
            if (host==null) {
                throw new ArgumentNullException("host");
            }
            if (authenticationType==null){
                throw new ArgumentNullException("authenticationType");
            }
            if (host.Length == 0) {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall, "host"));
            }
            if (port < 0) {
                throw new ArgumentOutOfRangeException("port");
            }


            GlobalLog.Print("CredentialCache::GetCredential(host=\"" + host + ":" + port.ToString() +"\", authenticationType=\"" + authenticationType + "\")");

            NetworkCredential match = null;

            IDictionaryEnumerator credEnum = cacheForHosts.GetEnumerator();

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

            while (credEnum.MoveNext()) {

                CredentialHostKey key = (CredentialHostKey)credEnum.Key;

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

                if (key.Match(host, port, authenticationType)) {

                        match = (NetworkCredential)credEnum.Value;
                }
            }

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

Same methods

CredentialCache::GetCredential ( Uri uriPrefix, string authType ) : 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