NegotiateStream.BeginAuthenticateAsClient 方法

定義

開始非同步作業以驗證用戶端與伺服器連接中的用戶端。

多載

BeginAuthenticateAsClient(AsyncCallback, Object)

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 這個方法不會封鎖。

BeginAuthenticateAsClient(NetworkCredential, String, AsyncCallback, Object)

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證。 這個方法不會封鎖。

BeginAuthenticateAsClient(NetworkCredential, ChannelBinding, String, AsyncCallback, Object)

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證和通道繫結。 這個方法不會封鎖。

BeginAuthenticateAsClient(NetworkCredential, String, ProtectionLevel, TokenImpersonationLevel, AsyncCallback, Object)

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證和驗證選項。 這個方法不會封鎖。

BeginAuthenticateAsClient(NetworkCredential, ChannelBinding, String, ProtectionLevel, TokenImpersonationLevel, AsyncCallback, Object)

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證、驗證選項和通道繫結。 這個方法不會封鎖。

備註

在進行驗證時,此方法的多載不會封鎖。 若要在等候驗證完成時封鎖,請使用其中 AuthenticateAsClient 一種方法。

BeginAuthenticateAsClient(AsyncCallback, Object)

來源:
NegotiateStream.cs
來源:
NegotiateStream.cs
來源:
NegotiateStream.cs

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 這個方法不會封鎖。

public:
 virtual IAsyncResult ^ BeginAuthenticateAsClient(AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (AsyncCallback? asyncCallback, object? asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (AsyncCallback asyncCallback, object asyncState);
abstract member BeginAuthenticateAsClient : AsyncCallback * obj -> IAsyncResult
override this.BeginAuthenticateAsClient : AsyncCallback * obj -> IAsyncResult
Public Overridable Function BeginAuthenticateAsClient (asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

參數

asyncCallback
AsyncCallback

AsyncCallback 委派,會於驗證完成時參考要叫用的方法。

asyncState
Object

包含作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

驗證失敗。 您可以使用這個物件重試驗證。

驗證失敗。 您可以使用這個物件重試驗證。

此物件已關閉。

已經進行驗證。

-或-

這個資料流先前是用來以伺服器的身分嘗試驗證。 您不能使用資料流以用戶端身分重試驗證。

範例

下列範例示範如何呼叫這個方法,以開始用戶端的異步驗證。

// Establish the remote endpoint for the socket.
// For this example, use the local machine.
IPHostEntry^ ipHostInfo = Dns::GetHostEntry( Dns::GetHostName() );
IPAddress^ ipAddress = ipHostInfo->AddressList[ 0 ];

// Client and server use port 11000. 
IPEndPoint^ remoteEP = gcnew IPEndPoint( ipAddress,11000 );

// Create a TCP/IP socket.
client = gcnew TcpClient;

// Connect the socket to the remote endpoint.
client->Connect( remoteEP );
Console::WriteLine( L"Client connected to {0}.", remoteEP );

// Ensure the client does not close when there is 
// still data to be sent to the server.
client->LingerState = (gcnew LingerOption( true,0 ));

// Request authentication.
NetworkStream^ clientStream = client->GetStream();
NegotiateStream^ authStream = gcnew NegotiateStream( clientStream,false );

// Pass the NegotiateStream as the AsyncState object 
// so that it is available to the callback delegate.
IAsyncResult^ ar = authStream->BeginAuthenticateAsClient( gcnew AsyncCallback( EndAuthenticateCallback ), authStream );

// Establish the remote endpoint for the socket.
// For this example, use the local machine.
IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
// Client and server use port 11000.
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
client = new TcpClient();
// Connect the socket to the remote endpoint.
client.Connect(remoteEP);
Console.WriteLine("Client connected to {0}.", remoteEP.ToString());
// Ensure the client does not close when there is
// still data to be sent to the server.
client.LingerState = new LingerOption(true, 0);
// Request authentication.
NetworkStream clientStream = client.GetStream();
NegotiateStream authStream = new NegotiateStream(clientStream, false);
// Pass the NegotiateStream as the AsyncState object
// so that it is available to the callback delegate.
Task authenticateTask = authStream
    .AuthenticateAsClientAsync()
    .ContinueWith(task =>
    {
        Console.WriteLine("Client ending authentication...");
        Console.WriteLine("ImpersonationLevel: {0}", authStream.ImpersonationLevel);
    });

' Establish the remote endpoint for the socket.
' For this example, use the local machine.
Dim ipHostInfo = Dns.GetHostEntry("localhost")
Dim ipAddress = ipHostInfo.AddressList(0)

' Client and server use port 11000. 
Dim remoteEP As New IPEndPoint(ipAddress, 11000)

' Create a TCP/IP socket.
client = New TcpClient()

' Connect the socket to the remote endpoint.
client.Connect(remoteEP)
Console.WriteLine("Client connected to {0}.", remoteEP.ToString())

' Ensure the client does not close when there is 
' still data to be sent to the server.
client.LingerState = (New LingerOption(True, 0))

' Request authentication.
Dim clientStream = client.GetStream()
Dim authStream As New NegotiateStream(clientStream, False)

' Pass the NegotiateStream as the AsyncState object 
' so that it is available to the callback delegate.
Dim ar = authStream.BeginAuthenticateAsClient(
    New AsyncCallback(AddressOf EndAuthenticateCallback), authStream)

備註

驗證會使用用戶端的 DefaultCredentials。 未為伺服器指定任何服務主體名稱 (SPN) 。 模擬層級為 Identification,且安全性層級為 EncryptAndSign。 類別 NegotiateStream 會建構用於相互驗證的SPN。

這個方法是異步的,而且作業完成時不會封鎖。 若要封鎖直到作業完成為止,請使用其中 AuthenticateAsClient 一個 方法多載。

異步驗證作業必須藉由呼叫 EndAuthenticateAsClient 方法來完成。 一般而言,委派會叫用 asyncCallback 方法。 如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

如果驗證失敗,您會收到 AuthenticationExceptionInvalidCredentialException。 在此情況下,您可以使用不同的認證重試驗證。

適用於

BeginAuthenticateAsClient(NetworkCredential, String, AsyncCallback, Object)

來源:
NegotiateStream.cs
來源:
NegotiateStream.cs
來源:
NegotiateStream.cs

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證。 這個方法不會封鎖。

public:
 virtual IAsyncResult ^ BeginAuthenticateAsClient(System::Net::NetworkCredential ^ credential, System::String ^ targetName, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, string targetName, AsyncCallback? asyncCallback, object? asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, string targetName, AsyncCallback asyncCallback, object asyncState);
abstract member BeginAuthenticateAsClient : System.Net.NetworkCredential * string * AsyncCallback * obj -> IAsyncResult
override this.BeginAuthenticateAsClient : System.Net.NetworkCredential * string * AsyncCallback * obj -> IAsyncResult
Public Overridable Function BeginAuthenticateAsClient (credential As NetworkCredential, targetName As String, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

參數

credential
NetworkCredential

建立用戶端識別所用的 NetworkCredential

targetName
String

服務主要名稱 (SPN),用來唯一識別要驗證的伺服器。

asyncCallback
AsyncCallback

AsyncCallback 委派,會於驗證完成時參考要叫用的方法。

asyncState
Object

包含寫入作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

credentialnull

-或-

targetNamenull

驗證失敗。 您可以使用這個物件重試驗證。

驗證失敗。 您可以使用這個物件重試驗證。

此物件已關閉。

已經進行驗證。

-或-

這個資料流先前是用來以伺服器的身分嘗試驗證。 您不能使用資料流以用戶端身分重試驗證。

備註

這個方法是異步的,而且作業完成時不會封鎖。 若要封鎖直到作業完成為止,請使用其中 AuthenticateAsClient 一個 方法多載。

異步驗證作業必須藉由呼叫 EndAuthenticateAsClient 方法來完成。 一般而言,委派會叫用 asyncCallback 方法。 如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

如果驗證失敗,您會收到 AuthenticationExceptionInvalidCredentialException。 在此情況下,您可以使用不同的認證重試驗證。

適用於

BeginAuthenticateAsClient(NetworkCredential, ChannelBinding, String, AsyncCallback, Object)

來源:
NegotiateStream.cs
來源:
NegotiateStream.cs
來源:
NegotiateStream.cs

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證和通道繫結。 這個方法不會封鎖。

public:
 virtual IAsyncResult ^ BeginAuthenticateAsClient(System::Net::NetworkCredential ^ credential, System::Security::Authentication::ExtendedProtection::ChannelBinding ^ binding, System::String ^ targetName, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, System.Security.Authentication.ExtendedProtection.ChannelBinding? binding, string targetName, AsyncCallback? asyncCallback, object? asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, System.Security.Authentication.ExtendedProtection.ChannelBinding binding, string targetName, AsyncCallback asyncCallback, object asyncState);
abstract member BeginAuthenticateAsClient : System.Net.NetworkCredential * System.Security.Authentication.ExtendedProtection.ChannelBinding * string * AsyncCallback * obj -> IAsyncResult
override this.BeginAuthenticateAsClient : System.Net.NetworkCredential * System.Security.Authentication.ExtendedProtection.ChannelBinding * string * AsyncCallback * obj -> IAsyncResult
Public Overridable Function BeginAuthenticateAsClient (credential As NetworkCredential, binding As ChannelBinding, targetName As String, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

參數

credential
NetworkCredential

建立用戶端識別所用的 NetworkCredential

binding
ChannelBinding

用於延伸保護的 ChannelBinding

targetName
String

服務主要名稱 (SPN),用來唯一識別要驗證的伺服器。

asyncCallback
AsyncCallback

AsyncCallback 委派,會於驗證完成時參考要叫用的方法。

asyncState
Object

包含寫入作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

credentialnull

-或-

targetNamenull

驗證失敗。 您可以使用這個物件重試驗證。

驗證失敗。 您可以使用這個物件重試驗證。

已經進行驗證。

-或-

這個資料流先前是用來以伺服器的身分嘗試驗證。 您不能使用資料流以用戶端身分重試驗證。

此物件已關閉。

備註

這個方法是異步的,而且作業完成時不會封鎖。 若要封鎖直到作業完成為止,請使用其中 AuthenticateAsClient 一個 方法多載。

異步驗證作業必須藉由呼叫 EndAuthenticateAsClient 方法來完成。 一般而言,委派會叫用 asyncCallback 方法。 如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

如果驗證失敗,您會收到 AuthenticationExceptionInvalidCredentialException。 在此情況下,您可以使用不同的認證重試驗證。

另請參閱

適用於

BeginAuthenticateAsClient(NetworkCredential, String, ProtectionLevel, TokenImpersonationLevel, AsyncCallback, Object)

來源:
NegotiateStream.cs
來源:
NegotiateStream.cs
來源:
NegotiateStream.cs

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證和驗證選項。 這個方法不會封鎖。

public:
 virtual IAsyncResult ^ BeginAuthenticateAsClient(System::Net::NetworkCredential ^ credential, System::String ^ targetName, System::Net::Security::ProtectionLevel requiredProtectionLevel, System::Security::Principal::TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, string targetName, System.Net.Security.ProtectionLevel requiredProtectionLevel, System.Security.Principal.TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback? asyncCallback, object? asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, string targetName, System.Net.Security.ProtectionLevel requiredProtectionLevel, System.Security.Principal.TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback asyncCallback, object asyncState);
abstract member BeginAuthenticateAsClient : System.Net.NetworkCredential * string * System.Net.Security.ProtectionLevel * System.Security.Principal.TokenImpersonationLevel * AsyncCallback * obj -> IAsyncResult
override this.BeginAuthenticateAsClient : System.Net.NetworkCredential * string * System.Net.Security.ProtectionLevel * System.Security.Principal.TokenImpersonationLevel * AsyncCallback * obj -> IAsyncResult
Public Overridable Function BeginAuthenticateAsClient (credential As NetworkCredential, targetName As String, requiredProtectionLevel As ProtectionLevel, allowedImpersonationLevel As TokenImpersonationLevel, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

參數

credential
NetworkCredential

建立用戶端識別所用的 NetworkCredential

targetName
String

服務主要名稱 (SPN),用來唯一識別要驗證的伺服器。

requiredProtectionLevel
ProtectionLevel

其中一個 ProtectionLevel 值,表示資料流的安全性服務。

allowedImpersonationLevel
TokenImpersonationLevel

其中一個 TokenImpersonationLevel 值,表示伺服器要如何使用用戶端的認證來存取資源。

asyncCallback
AsyncCallback

AsyncCallback 委派,會於驗證完成時參考要叫用的方法。

asyncState
Object

包含寫入作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

credentialnull

-或-

targetNamenull

驗證失敗。 您可以使用這個物件重試驗證。

驗證失敗。 您可以使用這個物件重試驗證。

此物件已關閉。

已經進行驗證。

-或-

這個資料流先前是用來以伺服器的身分嘗試驗證。 您不能使用資料流以用戶端身分重試驗證。

備註

requiredProtectionLevel使用 參數來要求使用已驗證數據流傳輸之數據的安全性服務。 例如,若要讓數據加密並簽署,請 EncryptAndSign 指定 值。 成功的驗證不保證已授與所要求的 ProtectionLevel 。 您必須檢查 IsEncryptedIsSigned 屬性,以判斷 所使用的 NegotiateStream安全性服務。

這個方法是異步的,而且作業完成時不會封鎖。 若要封鎖直到作業完成為止,請使用其中 AuthenticateAsClient 一個 方法多載。

異步驗證作業必須藉由呼叫 EndAuthenticateAsClient 方法來完成。 一般而言,委派會叫用 asyncCallback 方法。 如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

如果驗證失敗,您會收到 AuthenticationExceptionInvalidCredentialException。 在此情況下,您可以使用不同的認證重試驗證。

適用於

BeginAuthenticateAsClient(NetworkCredential, ChannelBinding, String, ProtectionLevel, TokenImpersonationLevel, AsyncCallback, Object)

來源:
NegotiateStream.cs
來源:
NegotiateStream.cs
來源:
NegotiateStream.cs

由用戶端呼叫以開始非同步作業,以便驗證用戶端與伺服器連接中的用戶端,並選擇性地驗證伺服器。 驗證處理序使用指定的認證、驗證選項和通道繫結。 這個方法不會封鎖。

public:
 virtual IAsyncResult ^ BeginAuthenticateAsClient(System::Net::NetworkCredential ^ credential, System::Security::Authentication::ExtendedProtection::ChannelBinding ^ binding, System::String ^ targetName, System::Net::Security::ProtectionLevel requiredProtectionLevel, System::Security::Principal::TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, System.Security.Authentication.ExtendedProtection.ChannelBinding? binding, string targetName, System.Net.Security.ProtectionLevel requiredProtectionLevel, System.Security.Principal.TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback? asyncCallback, object? asyncState);
public virtual IAsyncResult BeginAuthenticateAsClient (System.Net.NetworkCredential credential, System.Security.Authentication.ExtendedProtection.ChannelBinding binding, string targetName, System.Net.Security.ProtectionLevel requiredProtectionLevel, System.Security.Principal.TokenImpersonationLevel allowedImpersonationLevel, AsyncCallback asyncCallback, object asyncState);
abstract member BeginAuthenticateAsClient : System.Net.NetworkCredential * System.Security.Authentication.ExtendedProtection.ChannelBinding * string * System.Net.Security.ProtectionLevel * System.Security.Principal.TokenImpersonationLevel * AsyncCallback * obj -> IAsyncResult
override this.BeginAuthenticateAsClient : System.Net.NetworkCredential * System.Security.Authentication.ExtendedProtection.ChannelBinding * string * System.Net.Security.ProtectionLevel * System.Security.Principal.TokenImpersonationLevel * AsyncCallback * obj -> IAsyncResult
Public Overridable Function BeginAuthenticateAsClient (credential As NetworkCredential, binding As ChannelBinding, targetName As String, requiredProtectionLevel As ProtectionLevel, allowedImpersonationLevel As TokenImpersonationLevel, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

參數

credential
NetworkCredential

建立用戶端識別所用的 NetworkCredential

binding
ChannelBinding

用於延伸保護的 ChannelBinding

targetName
String

服務主要名稱 (SPN),用來唯一識別要驗證的伺服器。

requiredProtectionLevel
ProtectionLevel

其中一個 ProtectionLevel 值,表示資料流的安全性服務。

allowedImpersonationLevel
TokenImpersonationLevel

其中一個 TokenImpersonationLevel 值,表示伺服器要如何使用用戶端的認證來存取資源。

asyncCallback
AsyncCallback

AsyncCallback 委派,會於驗證完成時參考要叫用的方法。

asyncState
Object

包含寫入作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

credentialnull

-或-

targetNamenull

驗證失敗。 您可以使用這個物件重試驗證。

驗證失敗。 您可以使用這個物件重試驗證。

已經進行驗證。

-或-

這個資料流先前是用來以伺服器的身分嘗試驗證。 您不能使用資料流以用戶端身分重試驗證。

此物件已關閉。

備註

requiredProtectionLevel使用 參數來要求使用已驗證數據流傳輸之數據的安全性服務。 例如,若要讓數據加密並簽署,請 EncryptAndSign 指定 值。 成功的驗證不保證已授與所要求的 ProtectionLevel 。 您必須檢查 IsEncryptedIsSigned 屬性,以判斷 所使用的 NegotiateStream安全性服務。

這個方法是異步的,而且作業完成時不會封鎖。 若要封鎖直到作業完成為止,請使用其中 AuthenticateAsClient 一個 方法多載。

異步驗證作業必須藉由呼叫 EndAuthenticateAsClient 方法來完成。 一般而言,委派會叫用 asyncCallback 方法。 如需使用異步程序設計模型的詳細資訊,請參閱 以異步方式呼叫同步方法

如果驗證失敗,您會收到 AuthenticationExceptionInvalidCredentialException。 在此情況下,您可以使用不同的認證重試驗證。

另請參閱

適用於