NetworkCredential Constructor (String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the NetworkCredential class with the specified user name and password.

Namespace:   System.Net
Assembly:  System (in System.dll)

public NetworkCredential(
	string userName,
	string password


Type: System.String

The user name associated with the credentials.

Type: System.String

The password for the user name associated with the credentials.

The constructor initializes a NetworkCredential object with the UserName property set to userName and the Password property set to password.

The following code example creates a NetworkCredential object using the specified user name and password.

 // Call the onstructor  to create an instance of NetworkCredential with the 
 // specified user name and password.
 NetworkCredential myCredentials = new NetworkCredential(username,passwd);

// Create a WebRequest with the specified URL. 
WebRequest myWebRequest = WebRequest.Create(url);
myCredentials.Domain = domain;
myWebRequest.Credentials = myCredentials;
Console.WriteLine("\n\nCredentials Domain : {0} , UserName : {1} , Password : {2}",
myCredentials.Domain, myCredentials.UserName, myCredentials.Password);
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...");

// Send the request and wait for a response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Process the response.
Console.WriteLine("\nResponse received successfully.");
// Release the resources of the response object.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 3.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top