NetworkCredential Constructor
Initializes a new instance of the NetworkCredential class.
Overload List
Initializes a new instance of the NetworkCredential class.
Supported by the .NET Compact Framework.
[Visual Basic] Public Sub New()
[C#] public NetworkCredential();
[C++] public: NetworkCredential();
[JScript] public function NetworkCredential();
Initializes a new instance of the NetworkCredential class with the specified user name and password.
Supported by the .NET Compact Framework.
[Visual Basic] Public Sub New(String, String)
[C#] public NetworkCredential(string, string);
[C++] public: NetworkCredential(String*, String*);
[JScript] public function NetworkCredential(String, String);
Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.
Supported by the .NET Compact Framework.
[Visual Basic] Public Sub New(String, String, String)
[C#] public NetworkCredential(string, string, string);
[C++] public: NetworkCredential(String*, String*, String*);
[JScript] public function NetworkCredential(String, String, String);
Example
[Visual Basic, C#, C++] The following example creates a NetworkCredential instance using the specified user name and password.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of the NetworkCredential constructor. For other examples that might be available, see the individual overload topics.
[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
in the upper-left corner of the page.
See Also
NetworkCredential Class | NetworkCredential Members | System.Net Namespace