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 为每个未经身份验证的传入请求调用指定的委托,以确定用于对客户端进行身份验证的协议(如果有)。 GetContext仅当 成功对请求进行身份验证时, HttpListenerEndGetContext 方法才返回传入请求。 如果请求无法进行身份验证,则 HttpListener 会自动发送回 401 响应。 可以使用 属性获取已成功验证的客户端的 HttpRequest.LogonUserIdentity 标识。

如果希望 的实例 HttpListener 根据接收的请求的特征使用不同的身份验证协议 (例如请求的 UrlUserHostAddress 属性) ,那么将身份验证协议的选择委托给特定于应用程序的方法的功能非常有用。

注意

若要将此属性设置为启用 Digest、NTLM 或 Negotiate,需要 SecurityPermissionControlPrincipal

适用于