HttpListener.AuthenticationSchemes 属性

定义

获取或设置用于客户端身份验证的方案。Gets or sets the scheme used to authenticate clients.

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

属性值

AuthenticationSchemes

指示如何对客户端进行身份验证的 AuthenticationSchemes 枚举值的按位组合。A bitwise combination of AuthenticationSchemes enumeration values that indicates how clients are to be authenticated. 默认值是 AnonymousThe default value is Anonymous.

例外

此对象已关闭。This object has been closed.

示例

下面的代码示例演示如何使用 AuthenticationSchemes 属性来指定身份验证方案。The following code example demonstrates using the AuthenticationSchemes property to specify an authentication scheme.

public static void SimpleListenerWithUnsafeAuthentication(string[] prefixes)
{
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");
    // Set up a listener.
    HttpListener listener = new HttpListener();
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    // Specify Negotiate as the authentication scheme.
    listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
    // If NTLM is used, we will allow multiple requests on the same
    // connection to use the authentication information of first request.
    // This improves performance but does reduce the security of your
    // application.
    listener.UnsafeConnectionNtlmAuthentication = true;
    // This listener does not want to receive exceptions
    // that occur when sending the response to the client.
    listener.IgnoreWriteExceptions = true;
    Console.WriteLine("Listening...");
    // ... process requests here.

    listener.Close();
}
Public Shared Sub SimpleListenerWithUnsafeAuthentication(ByVal prefixes As String())
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    If prefixes Is Nothing OrElse prefixes.Length = 0 Then Throw New ArgumentException("prefixes")
    ' Set up a listener.
    Dim listener As HttpListener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next

    listener.Start()
    ' Specify Negotiate as the authentication scheme.
    listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate
    ' If NTLM Is used, we will allow multiple requests on the same
    ' connection to use the authentication information of first request.
    ' This improves performance but does reduce the security of your 
    ' application. 
    listener.UnsafeConnectionNtlmAuthentication = True
    ' This listener does Not want to receive exceptions 
    ' that occur when sending the response to the client.
    listener.IgnoreWriteExceptions = True
    Console.WriteLine("Listening...")
    ' ... process requests here.

    listener.Close()
End Sub

注解

HttpListener使用指定的方案对所有传入的请求进行身份验证。The HttpListener uses the specified scheme to authenticate all incoming requests. GetContext EndGetContext 仅当 HttpListener 成功对请求进行身份验证时,和方法才返回传入的客户端请求。The GetContext and EndGetContext methods return an incoming client request only if the HttpListener successfully authenticates the request.

可以通过使用属性来询问已成功通过身份验证的客户端的标识 HttpListenerContext.UserYou can interrogate the identity of a successfully authenticated client by using the HttpListenerContext.User property.

如果希望 HttpListener 对象根据其收到的请求的特征使用不同的身份验证机制 (例如,请求的 UrlUserHostName 属性) ,则必须实现选择身份验证方案的方法。If you want an HttpListener object to use different authentication mechanisms based on characteristics of the requests it receives (for example, the request's Url or UserHostName property), you must implement a method that chooses the authentication scheme. 有关如何执行此操作的说明,请参阅 AuthenticationSchemeSelectorDelegate 属性文档。For instructions about how to do this, see the AuthenticationSchemeSelectorDelegate property documentation.

备注

若要设置此属性以启用 Digest、NTLM 或 Negotiate,需要 SecurityPermissionControlPrincipalTo set this property to enable Digest, NTLM, or Negotiate requires the SecurityPermission, ControlPrincipal.

适用于