CredentialCache.GetCredential Method

Definition

Returns a NetworkCredential instance associated with a specified Uniform Resource Identifier (URI) or host, and authentication type.

Overloads

GetCredential(Uri, String)

Returns the NetworkCredential instance associated with the specified Uniform Resource Identifier (URI) and authentication type.

GetCredential(String, Int32, String)

Returns the NetworkCredential instance associated with the specified host, port, and authentication protocol.

GetCredential(Uri, String)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

Returns the NetworkCredential instance associated with the specified Uniform Resource Identifier (URI) and authentication type.

C#
public System.Net.NetworkCredential GetCredential(Uri uriPrefix, string authType);
C#
public System.Net.NetworkCredential? GetCredential(Uri uriPrefix, string authType);

Parameters

uriPrefix
Uri

A Uri that specifies the URI prefix of the resources that the credential grants access to.

authType
String

The authentication scheme used by the resource named in uriPrefix.

Returns

A NetworkCredential or, if there is no matching credential in the cache, null.

Implements

Exceptions

uriPrefix or authType is null.

Examples

The following code example uses the GetCredential(Uri, String) method to return the NetworkCredential instance associated with the specified URI and authentication type.

C#
  public static void GetPage(string url,string userName,string password,string domainName)
  {
      try
      {
          CredentialCache myCredentialCache = new CredentialCache();
          // Dummy names used as credentials.
          myCredentialCache.Add(new Uri("http://microsoft.com/"),"Basic", new NetworkCredential("user1","passwd1","domain1"));
          myCredentialCache.Add(new Uri("http://msdn.com/"),"Basic", new NetworkCredential("user2","passwd2","domain2"));
          myCredentialCache.Add(new Uri(url),"Basic", new NetworkCredential(userName,password,domainName));
          // Create a webrequest with the specified url.
       WebRequest myWebRequest = WebRequest.Create(url);
          // Call 'GetCredential' to obtain the credentials specific to our Uri.
          NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri(url),"Basic");
          Display(myCredential);
       // Associating only our credentials.
          myWebRequest.Credentials = myCredential;
          // Sends the request and waits for response.
       WebResponse myWebResponse = myWebRequest.GetResponse();

          // Process response here.

       Console.WriteLine("\nResponse Received.");
          myWebResponse.Close();
      }
      catch(WebException e)
      {
          if (e.Response != null)
              Console.WriteLine("\r\nFailed to obtain a response. The following error occurred : {0}",((HttpWebResponse)(e.Response)).StatusDescription);
          else
              Console.WriteLine("\r\nFailed to obtain a response. The following error occurred : {0}",e.Status);
      }
      catch(Exception e)
      {
          Console.WriteLine("\nThe following exception was raised : {0}",e.Message);
      }
}
public static void Display(NetworkCredential credential)
{
  Console.WriteLine("\nThe credentials are:");
  Console.WriteLine("\nUsername : {0} ,Password : {1} ,Domain : {2}",credential.UserName,credential.Password,credential.Domain);
}

Remarks

The GetCredential(Uri, String) method searches the CredentialCache and returns the NetworkCredential instance for the specified URI and authorization type. If the CredentialCache contains no matching NetworkCredential instance, null is returned.

GetCredential uses the longest matching URI prefix in the cache to determine which set of credentials to return for an authorization type. The following table shows examples.

URI Prefix Matches
http://www.contoso.com/portal/news.htm Requests for the specific Web page news.htm.
http://www.contoso.com/portal/ Requests for all content in the portal path, except the page news.htm.
http://www.contoso.com/ Requests for all resources at www.contoso.com, except those in the portal path.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCredential(String, Int32, String)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

Returns the NetworkCredential instance associated with the specified host, port, and authentication protocol.

C#
public System.Net.NetworkCredential GetCredential(string host, int port, string authenticationType);
C#
public System.Net.NetworkCredential? GetCredential(string host, int port, string authenticationType);

Parameters

host
String

A String that identifies the host computer.

port
Int32

A Int32 that specifies the port to connect to on host.

authenticationType
String

A String that identifies the authentication scheme used when connecting to host.

Returns

A NetworkCredential or, if there is no matching credential in the cache, null.

Implements

Exceptions

host is null.

-or-

authenticationType is null.

authenticationType not an accepted value.

-or-

host is equal to the empty string ("").

port is less than zero.

Remarks

This method searches the CredentialCache and returns the NetworkCredential instance for the specified host, port, and authorization type. The host, port, and authenticationType values passed to this method are case-insensitively compared to the values specified when the credential was added to the CredentialCache using the Add methods.

The supported values for authenticationType are "NTLM", "Digest", "Kerberos", and "Negotiate".

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0