NetworkCredential 构造函数

定义

初始化 NetworkCredential 类的新实例。

重载

NetworkCredential()

初始化 NetworkCredential 类的新实例。

NetworkCredential(String, SecureString)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。

NetworkCredential(String, String)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。

NetworkCredential(String, SecureString, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。

NetworkCredential(String, String, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。

NetworkCredential()

初始化 NetworkCredential 类的新实例。

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

注解

类的 NetworkCredential 无参数构造函数将所有属性初始化为 null.

适用于

NetworkCredential(String, SecureString)

重要

此 API 不符合 CLS。

用指定的用户名和密码初始化 NetworkCredential 类的新实例。

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

参数

userName
String

与凭据关联的用户名。

password
SecureString

与凭据关联的用户名的密码。

属性

例外

SecureString 在此平台上不受支持。

注解

构造函数使用NetworkCredential属性集初始化userName对象UserNamePassword并将属性设置为 password

参数 password 是一个 SecureString 实例。

如果使用参数设置为 null 调用password此构造函数,则初始化新实例SecureString;如果此平台上不支持安全字符串,则会NotSupportedException引发

适用于

NetworkCredential(String, String)

用指定的用户名和密码初始化 NetworkCredential 类的新实例。

public:
 NetworkCredential(System::String ^ userName, System::String ^ password);
public NetworkCredential (string userName, 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

与凭据关联的用户名。

password
String

与凭据关联的用户名的密码。

示例

下面的代码示例使用指定的用户名和密码创建一个 NetworkCredential 对象。

// 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 constructor 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对象UserNamePassword并将属性设置为 password

适用于

NetworkCredential(String, SecureString, String)

重要

此 API 不符合 CLS。

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。

public:
 NetworkCredential(System::String ^ userName, System::Security::SecureString ^ password, System::String ^ domain);
[System.CLSCompliant(false)]
public NetworkCredential (string? userName, System.Security.SecureString? password, string? domain);
[System.CLSCompliant(false)]
public NetworkCredential (string userName, System.Security.SecureString password, string domain);
public NetworkCredential (string userName, System.Security.SecureString password, string domain);
[<System.CLSCompliant(false)>]
new System.Net.NetworkCredential : string * System.Security.SecureString * string -> System.Net.NetworkCredential
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

与凭据关联的用户名。

password
SecureString

与凭据关联的用户名的密码。

domain
String

与这些凭据关联的域。

属性

例外

SecureString 在此平台上不受支持。

注解

构造函数使用NetworkCredentialUserName属性集设置为userNamePassword属性设置为passwordDomain属性设置为的对象初始化domain

参数 password 是一个 SecureString 实例。

如果使用参数设置为 null 调用password此构造函数,则初始化新实例SecureString;如果此平台上不支持安全字符串,则会NotSupportedException引发

适用于

NetworkCredential(String, String, String)

用指定的用户名、密码和域初始化 NetworkCredential 类的新实例。

public:
 NetworkCredential(System::String ^ userName, System::String ^ password, System::String ^ domain);
public NetworkCredential (string userName, string password, 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

与凭据关联的用户名。

password
String

与凭据关联的用户名的密码。

domain
String

与这些凭据关联的域。

注解

构造函数使用NetworkCredentialUserName属性集设置为userNamePassword属性设置为passwordDomain属性设置为的对象初始化domain

适用于