AuthenticationSchemeSelector 委托

定义

HttpListener 实例选择身份验证方案。

public delegate System::Net::AuthenticationSchemes AuthenticationSchemeSelector(HttpListenerRequest ^ httpRequest);
public delegate System.Net.AuthenticationSchemes AuthenticationSchemeSelector(HttpListenerRequest httpRequest);
type AuthenticationSchemeSelector = delegate of HttpListenerRequest -> AuthenticationSchemes
Public Delegate Function AuthenticationSchemeSelector(httpRequest As HttpListenerRequest) As AuthenticationSchemes 

参数

httpRequest
HttpListenerRequest

要为其选择身份验证方案的 HttpListenerRequest 实例。

返回值

AuthenticationSchemes 值之一,指示要用于指定的客户端请求的身份验证方法。

示例

以下示例使用此类型的实例来设置 AuthenticationSchemeSelectorDelegate 属性。

// 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 类型的委托根据请求的特征选择身份验证方案。

委托 AuthenticationSchemeSelector 将为每个未提供身份验证信息的传入请求传递对象 HttpListenerRequest 。 委托调用的方法使用 HttpListenerRequest 对象和任何其他可用信息来确定需要哪种身份验证方案。 委托是使用 AuthenticationSchemeSelectorDelegate 属性指定的。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于