NetworkCredential 构造函数

定义

初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class.

重载

NetworkCredential()

初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class.

NetworkCredential(String, SecureString)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name and password.

NetworkCredential(String, String)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name and password.

NetworkCredential(String, SecureString, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.

NetworkCredential(String, String, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.

NetworkCredential()

初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class.

public:
 NetworkCredential();
public NetworkCredential ();
Public Sub New ()

注解

NetworkCredential 类的无参数构造函数将所有属性初始化为 nullThe parameterless constructor for the NetworkCredential class initializes all properties to null.

NetworkCredential(String, SecureString)

重要

此 API 不符合 CLS。

用指定的用户名和密码初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name and password.

public:
 NetworkCredential(System::String ^ userName, System::Security::SecureString ^ password);
public NetworkCredential (string userName, System.Security.SecureString password);
[System.CLSCompliant(false)]
public NetworkCredential (string userName, System.Security.SecureString password);
new System.Net.NetworkCredential : string * System.Security.SecureString -> System.Net.NetworkCredential
Public Sub New (userName As String, password As SecureString)

参数

userName
String

与凭据关联的用户名。The user name associated with the credentials.

password
SecureString

与凭据关联的用户名的密码。The password for the user name associated with the credentials.

属性

例外

SecureString 在此平台上不受支持。The SecureString class is not supported on this platform.

注解

构造函数将初始化一个 NetworkCredential 对象,该对象的 UserName 属性设置为 userName,并将 Password 属性设置为 passwordThe constructor initializes a NetworkCredential object with the UserName property set to userName and the Password property set to password.

password 参数是 SecureString 实例。The password parameter is a SecureString instance.

如果在 password 参数设置为 null的情况下调用此构造函数,则将初始化 SecureString 的新实例,如果此平台不支持安全字符串,则会引发 NotSupportedExceptionIf this constructor is called with the password parameter set to null, a new instance of SecureString is initialized, If secure strings are not supported on this platform, then the NotSupportedException is thrown

NetworkCredential(String, String)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name and password.

public:
 NetworkCredential(System::String ^ userName, System::String ^ password);
public NetworkCredential (string userName, string password);
new System.Net.NetworkCredential : string * string -> System.Net.NetworkCredential
Public Sub New (userName As String, password As String)

参数

userName
String

与凭据关联的用户名。The user name associated with the credentials.

password
String

与凭据关联的用户名的密码。The password for the user name associated with the credentials.

示例

下面的代码示例使用指定的用户名和密码创建 NetworkCredential 对象。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 = 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.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 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()

注解

构造函数将初始化一个 NetworkCredential 对象,该对象的 UserName 属性设置为 userName,并将 Password 属性设置为 passwordThe constructor initializes a NetworkCredential object with the UserName property set to userName and the Password property set to password.

NetworkCredential(String, SecureString, String)

重要

此 API 不符合 CLS。

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.

public:
 NetworkCredential(System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
public NetworkCredential (string userName, System.Security.SecureString password, string domain);
[System.CLSCompliant(false)]
public NetworkCredential (string userName, System.Security.SecureString password, string domain);
new System.Net.NetworkCredential : string * System.Security.SecureString * string -> System.Net.NetworkCredential
Public Sub New (userName As String, password As SecureString, domain As String)

参数

userName
String

与凭据关联的用户名。The user name associated with the credentials.

password
SecureString

与凭据关联的用户名的密码。The password for the user name associated with the credentials.

domain
String

与这些凭据关联的域。The domain associated with these credentials.

属性

例外

SecureString 在此平台上不受支持。The SecureString class is not supported on this platform.

注解

构造函数会初始化一个 NetworkCredential 对象,该对象的 UserName 属性设置为 userNamePassword 属性设置为 password,而 Domain 属性设置为 domainThe constructor initializes a NetworkCredential object with the UserName property set to userName, the Password property set to password, and the Domain property set to domain.

password 参数是 SecureString 实例。The password parameter is a SecureString instance.

如果在 password 参数设置为 null的情况下调用此构造函数,则将初始化 SecureString 的新实例,如果此平台不支持安全字符串,则会引发 NotSupportedExceptionIf this constructor is called with the password parameter set to null, a new instance of SecureString is initialized, If secure strings are not supported on this platform, then the NotSupportedException is thrown

NetworkCredential(String, String, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.

public:
 NetworkCredential(System::String ^ userName, System::String ^ password, System::String ^ domain);
public NetworkCredential (string userName, string password, string domain);
new System.Net.NetworkCredential : string * string * string -> System.Net.NetworkCredential
Public Sub New (userName As String, password As String, domain As String)

参数

userName
String

与凭据关联的用户名。The user name associated with the credentials.

password
String

与凭据关联的用户名的密码。The password for the user name associated with the credentials.

domain
String

与这些凭据关联的域。The domain associated with these credentials.

注解

构造函数会初始化一个 NetworkCredential 对象,该对象的 UserName 属性设置为 userNamePassword 属性设置为 password,而 Domain 属性设置为 domainThe constructor initializes a NetworkCredential object with the UserName property set to userName, the Password property set to password, and the Domain property set to domain.

适用于