HttpListener.AuthenticationSchemeSelectorDelegate 屬性

定義

取得或設定呼叫的委派,以決定驗證用戶端時使用的通訊協定。Gets or sets the delegate called to determine the protocol used to authenticate clients.

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

屬性值

AuthenticationSchemeSelector 委派,叫用用來選擇驗證通訊協定的方法。An AuthenticationSchemeSelector delegate that invokes the method used to select an authentication protocol. 預設值為 nullThe default value is null.

例外狀況

此物件已關閉。This object has been closed.

範例

下列程式碼範例會設定這個屬性的值。The following code example sets the value of this property.

// 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 委派所叫用之方法的執行。The following code example provides an implementation of a method invoked by an AuthenticationSchemeSelector delegate.

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特定實例所處理的所有要求,則不需要設定此屬性。If you want the same authentication protocol to be used for all requests handled by a particular instance of HttpListener, you do not need to set this property. 若要指定要用於所有用戶端要求的通訊協定,請使用 AuthenticationSchemes 屬性。To specify a protocol to be used for all client requests, use the AuthenticationSchemes property.

如果用戶端未在其標頭中指定驗證資訊,則 HttpListener 會針對每個未驗證的連入要求呼叫指定的委派,以判斷要使用哪一個(如果有的話)通訊協定來驗證用戶端。If the client has not specified authentication information in its headers, the HttpListener calls the specified delegate for each unauthenticated incoming request to determine which, if any, protocol to use to authenticate the client. 只有在 HttpListener 成功驗證要求時,GetContextEndGetContext 方法才會傳回傳入要求。The GetContext and EndGetContext methods return an incoming request only if the HttpListener successfully authenticated the request. 如果無法驗證要求,HttpListener 會自動傳回401回應。If a request cannot be authenticated, the HttpListener automatically sends back a 401 response. 您可以使用 HttpRequest.LogonUserIdentity 屬性來取得已成功驗證之用戶端的身分識別。You can get the identity of a successfully authenticated client using the HttpRequest.LogonUserIdentity property.

如果您想要 HttpListener 的實例使用不同的驗證通訊協定(視它所收到的要求特性而定),則可以將驗證通訊協定的選擇委派給應用程式特定的方法會很有用(例如,要求的 UrlUserHostAddress 屬性)。The ability to delegate the choice of authentication protocol to an application-specific method is useful if you want an instance of HttpListener to use different authentication protocols depending on the characteristics of the requests it receives (for example, the request's Url or UserHostAddress property).

注意

若要將此屬性設定為啟用摘要式、NTLM 或 Negotiate,需要 SecurityPermissionControlPrincipalTo set this property to enable Digest, NTLM, or Negotiate requires the SecurityPermission, ControlPrincipal.

適用於