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 委派,叫用用來選取驗證通訊協定的方法。 預設值是 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 需要 SecurityPermissionControlPrincipal

適用於