Freigeben über


NetworkCredential-Konstruktor (String, String)

Initialisiert eine neue Instanz der NetworkCredential-Klasse mit dem angegebenen Benutzernamen und Kennwort.

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

Syntax

'Declaration
Public Sub New ( _
    userName As String, _
    password As String _
)
'Usage
Dim userName As String
Dim password As String

Dim instance As New NetworkCredential(userName, password)
public NetworkCredential (
    string userName,
    string password
)
public:
NetworkCredential (
    String^ userName, 
    String^ password
)
public NetworkCredential (
    String userName, 
    String password
)
public function NetworkCredential (
    userName : String, 
    password : String
)

Parameter

  • userName
    Der den Anmeldeinformationen zugeordnete Benutzername.
  • password
    Das Kennwort für den Benutzernamen, der den Anmeldeinformationen zugeordnet ist.

Hinweise

Beim Initialisieren eines NetworkCredential-Objekts legt der Konstruktor die UserName-Eigenschaft auf userName und die Password-Eigenschaft auf password fest.

Beispiel

Im folgenden Codebeispiel wird ein NetworkCredential-Objekt mit dem angegebenen Benutzernamen und Kennwort erstellt.

' 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()
 // 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();
// Call the onstructor  to create an instance of NetworkCredential with the
// specified user name and password.
NetworkCredential^ myCredentials = gcnew 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();
// 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.set_Domain(domain);
myWebRequest.set_Credentials(myCredentials);
Console.WriteLine("\n\nCredentials Domain : {0} , UserName : "
    + "{1} , Password : {2}", myCredentials.get_Domain(),
    myCredentials.get_UserName(), myCredentials.get_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();

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

NetworkCredential-Klasse
NetworkCredential-Member
System.Net-Namespace