NetworkCredential.GetCredential Method

Definition

Returns an instance of the NetworkCredential class for the specified authentication type.

Overloads

GetCredential(String, Int32, String)

Returns an instance of the NetworkCredential class for the specified host, port, and authentication type.

GetCredential(Uri, String)

Returns an instance of the NetworkCredential class for the specified Uniform Resource Identifier (URI) and authentication type.

GetCredential(String, Int32, String)

Source:
NetworkCredential.cs
Source:
NetworkCredential.cs
Source:
NetworkCredential.cs

Returns an instance of the NetworkCredential class for the specified host, port, and authentication type.

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

Parameters

host
String

The host computer that authenticates the client.

port
Int32

The port on the host that the client communicates with.

authenticationType
String

The type of authentication requested, as defined in the AuthenticationType property.

Returns

A NetworkCredential for the specified host, port, and authentication protocol, or null if there are no credentials available for the specified host, port, and authentication protocol.

Implements

Remarks

The value of authType corresponds to the IAuthenticationModule.AuthenticationType property.

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

GetCredential(Uri, String)

Source:
NetworkCredential.cs
Source:
NetworkCredential.cs
Source:
NetworkCredential.cs

Returns an instance of the NetworkCredential class for the specified Uniform Resource Identifier (URI) and authentication type.

public System.Net.NetworkCredential GetCredential (Uri uri, string authType);
public System.Net.NetworkCredential GetCredential (Uri? uri, string? authenticationType);

Parameters

uri
Uri

The URI that the client provides authentication for.

authTypeauthenticationType
String

The type of authentication requested, as defined in the AuthenticationType property.

Returns

A NetworkCredential object.

Implements

Examples

The following code example uses the GetCredential method to retrieve a NetworkCredential object for the specified URI.

// Create an empty instance of the NetworkCredential class.
NetworkCredential myCredentials = new NetworkCredential(userName,password);
// Create a webrequest with the specified URL.
WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Credentials = myCredentials.GetCredential(new Uri(url),"");
Console.WriteLine("\n\nUser Credentials:- UserName : {0} , Password : {1}",myCredentials.UserName,myCredentials.Password);
// Send the request and wait for a response.
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...Please wait ...");
WebResponse myWebResponse = myWebRequest.GetResponse();
// Process the response.
   Console.WriteLine("\nResponse received successfully");
// Release the resources of the response object.
myWebResponse.Close();

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.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0