Export (0) Print
Expand All

NetworkCredential Constructor (String, String)

.NET Framework 1.1

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

[Visual Basic]
Public Sub New( _
   ByVal userName As String, _
   ByVal password As String _
)
[C#]
public NetworkCredential(
   string userName,
 string password
);
[C++]
public: NetworkCredential(
   String* userName,
 String* password
);
[JScript]
public function NetworkCredential(
   userName : String,
 password : String
);

Parameters

userName
The user name associated with the credentials.
password
The password for the user name associated with the credentials.

Remarks

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

Example

[Visual Basic, C#, C++] The following example creates a NetworkCredential instance using the specified user name and password.

[Visual Basic] 
' Call the constructor  to create an instance of NetworkCredential with the
' specified user name and password.
Dim myCredentials As New NetworkCredential(username, passwd)
' Create a WebRequest with the specified URL. 
Dim myWebRequest As WebRequest = WebRequest.Create(url)
myCredentials.Domain = domain
myWebRequest.Credentials = myCredentials
Console.WriteLine(ControlChars.Cr + ControlChars.Cr + "Credentials Domain : {0} , UserName : {1} , Password : {2}", myCredentials.Domain, myCredentials.UserName, myCredentials.Password)
Console.WriteLine(ControlChars.Cr + ControlChars.Cr + "Request to Url is sent.Waiting for response...")
' Send the request and wait for a response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
' Process the response.
Console.WriteLine(ControlChars.Cr + "Response received successfully.")
' Release the resources of the response object.
myWebResponse.Close()

[C#] 
// 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.
      myWebResponse.Close();

[C++] 
// 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(S"\n\nCredentials Domain : {0} , UserName : {1} , Password : {2}",
   myCredentials->Domain, myCredentials->UserName, myCredentials->Password);
Console::WriteLine(S"\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(S"\nResponse received successfully.");
// Release the resources of the response object.
myWebResponse->Close();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

NetworkCredential Class | NetworkCredential Members | System.Net Namespace | NetworkCredential Constructor Overload List

Show:
© 2015 Microsoft