共用方式為


HttpListener.AuthenticationSchemes 屬性

定義

取得或設定用來驗證用戶端的配置。

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 列舉值的位元組合,表示用戶端的驗證方式。 預設值是 Anonymous

例外狀況

此物件已關閉。

範例

下列程式代碼範例示範如何使用 AuthenticationSchemes 屬性來指定驗證配置。

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 使用指定的配置來驗證所有傳入要求。 GetContextEndGetContext 方法只有在成功驗證要求時HttpListener,才會傳回傳入用戶端要求。

您可以使用 屬性來詢問成功驗證用戶端 HttpListenerContext.User 的身分識別。

如果您想要 HttpListener 根據物件接收的要求特性使用不同的驗證機制,例如要求 (UrlUserHostName 屬性) ,您必須實作選擇驗證配置的方法。 如需如何執行這項操作的指示,請參閱 AuthenticationSchemeSelectorDelegate 屬性檔。

注意

若要將此屬性設定為啟用 Digest、NTLM 或 Negotiate 需要 SecurityPermissionControlPrincipal

適用於