HttpListener.AuthenticationSchemes HttpListener.AuthenticationSchemes HttpListener.AuthenticationSchemes HttpListener.AuthenticationSchemes Property

定義

取得或設定用來驗證用戶端的配置。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 列舉值的位元組合,指定用戶端的驗證方式。A bitwise combination of AuthenticationSchemes enumeration values that indicates how clients are to be authenticated. 預設值為 AnonymousThe default value is Anonymous.

例外狀況

範例

下列程式碼範例將示範如何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. 只有在EndGetContext GetContext 成功HttpListener驗證要求時, 和方法才會傳回傳入的用戶端要求。The GetContext and EndGetContext methods return an incoming client request only if the HttpListener successfully authenticates the request.

您可以使用HttpListenerContext.User屬性來詢問已成功驗證之用戶端的身分識別。You 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.

注意

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

適用於