HttpListener.AuthenticationSchemeSelectorDelegate 屬性

定義

取得或設定委派,呼叫這個委派可決定用來驗證用戶端的通訊協定。

public:
 property System::Net::AuthenticationSchemeSelector ^ AuthenticationSchemeSelectorDelegate { System::Net::AuthenticationSchemeSelector ^ get(); void set(System::Net::AuthenticationSchemeSelector ^ value); };
public System.Net.AuthenticationSchemeSelector? AuthenticationSchemeSelectorDelegate { get; set; }
public System.Net.AuthenticationSchemeSelector AuthenticationSchemeSelectorDelegate { get; set; }
member this.AuthenticationSchemeSelectorDelegate : System.Net.AuthenticationSchemeSelector with get, set
Public Property AuthenticationSchemeSelectorDelegate As AuthenticationSchemeSelector

屬性值

AuthenticationSchemeSelector

AuthenticationSchemeSelector 委派,叫用用來選取驗證通訊協定的方法。 預設值是 null

例外狀況

此物件已關閉。

範例

下列程式碼範例會設定此屬性的值。

// Set up a listener.
HttpListener listener = new HttpListener();
HttpListenerPrefixCollection prefixes = listener.Prefixes;
prefixes.Add(@"http://localhost:8080/");
prefixes.Add(@"http://contoso.com:8080/");

// Specify the authentication delegate.
listener.AuthenticationSchemeSelectorDelegate =
    new AuthenticationSchemeSelector (AuthenticationSchemeForClient);

// Start listening for requests and process them
// synchronously.
listener.Start();
' Set up a listener.
Dim listener As New HttpListener()
Dim prefixes As HttpListenerPrefixCollection = listener.Prefixes
prefixes.Add("http://localhost:8080/")
prefixes.Add("http://contoso.com:8080/")

' Specify the authentication delegate.
listener.AuthenticationSchemeSelectorDelegate = New AuthenticationSchemeSelector(AddressOf AuthenticationSchemeForClient)

' Start listening for requests and process them 
' synchronously.
listener.Start()

下列程式碼範例提供委派所叫用方法的 AuthenticationSchemeSelector 實作。

static AuthenticationSchemes AuthenticationSchemeForClient(HttpListenerRequest request)
{
    Console.WriteLine("Client authentication protocol selection in progress...");
    // Do not authenticate local machine requests.
    if (request.RemoteEndPoint.Address.Equals (IPAddress.Loopback))
    {
        return AuthenticationSchemes.None;
    }
    else
    {
        return AuthenticationSchemes.IntegratedWindowsAuthentication;
    }
}
Private Shared Function AuthenticationSchemeForClient(ByVal request As HttpListenerRequest) As AuthenticationSchemes
    Console.WriteLine("Client authentication protocol selection in progress...")
    ' Do not authenticate local machine requests.
    If request.RemoteEndPoint.Address.Equals(IPAddress.Loopback) Then
        Return AuthenticationSchemes.None
    Else
        Return AuthenticationSchemes.IntegratedWindowsAuthentication
    End If
End Function

備註

注意

如果您想要將相同的驗證通訊協定用於特定 實例 HttpListener 所處理的所有要求,則不需要設定此屬性。 若要指定要用於所有用戶端要求的通訊協定,請使用 AuthenticationSchemes 屬性。

如果用戶端在其標頭中未指定驗證資訊,則會 HttpListener 針對每個未經驗證的連入要求呼叫指定的委派,以判斷要使用哪一種通訊協定來驗證用戶端。 GetContextEndGetContext 方法只有在成功驗證要求時 HttpListener ,才會傳回傳入要求。 如果無法驗證要求,則 HttpListener 會自動傳回 401 回應。 您可以使用 屬性取得成功驗證用戶端 HttpRequest.LogonUserIdentity 的身分識別。

如果您希望 的實例 HttpListener 使用不同的驗證通訊協定,視它 (接收的要求特性而定,將驗證通訊協定的選擇委派給應用程式特定的方法會很有用,例如,要求的 UrlUserHostAddress 屬性) 。

注意

若要設定此屬性以啟用 Digest、NTLM 或 Negotiate, SecurityPermission 需要 、 ControlPrincipal

適用於