NetworkCredential.GetCredential 方法

定義

傳回指定驗證類型之 NetworkCredential 類別的執行個體。

多載

GetCredential(String, Int32, String)

傳回指定主機、連接埠和驗證類型之 NetworkCredential 類別的執行個體。

GetCredential(Uri, String)

傳回指定統一資源識別元 (URI) 和驗證類型之 NetworkCredential 類別的執行個體。

GetCredential(String, Int32, String)

來源:
NetworkCredential.cs
來源:
NetworkCredential.cs
來源:
NetworkCredential.cs

傳回指定主機、連接埠和驗證類型之 NetworkCredential 類別的執行個體。

C#
public System.Net.NetworkCredential GetCredential (string host, int port, string authenticationType);
C#
public System.Net.NetworkCredential GetCredential (string? host, int port, string? authenticationType);

參數

host
String

驗證用戶端的主機電腦。

port
Int32

用戶端將與之通訊的 host 連接埠。

authenticationType
String

要求驗證的類型,如同在 AuthenticationType 屬性中的定義。

傳回

指定主機、連接埠和驗證通訊協定的 NetworkCredential,如果指定主機、連接埠和驗證通訊協定沒有認證,則為 null

實作

備註

的值 authType 會對應至 IAuthenticationModule.AuthenticationType 屬性。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCredential(Uri, String)

來源:
NetworkCredential.cs
來源:
NetworkCredential.cs
來源:
NetworkCredential.cs

傳回指定統一資源識別元 (URI) 和驗證類型之 NetworkCredential 類別的執行個體。

C#
public System.Net.NetworkCredential GetCredential (Uri uri, string authType);
C#
public System.Net.NetworkCredential GetCredential (Uri? uri, string? authenticationType);

參數

uri
Uri

用戶端為其提供驗證的 URI。

authTypeauthenticationType
String

要求驗證的類型,如同在 AuthenticationType 屬性中的定義。

傳回

NetworkCredential 物件。

實作

範例

下列程式代碼範例會 GetCredential 使用 方法來擷取 NetworkCredential 指定 URI 的物件。

C#
// Create an empty instance of the NetworkCredential class.
NetworkCredential myCredentials = new NetworkCredential(userName,password);
// Create a webrequest with the specified URL.
WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Credentials = myCredentials.GetCredential(new Uri(url),"");
Console.WriteLine("\n\nUser Credentials:- UserName : {0} , Password : {1}",myCredentials.UserName,myCredentials.Password);
// Send the request and wait for a response.
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...Please wait ...");
WebResponse myWebResponse = myWebRequest.GetResponse();
// Process the response.
   Console.WriteLine("\nResponse received successfully");
// Release the resources of the response object.
myWebResponse.Close();

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0