閱讀英文

共用方式為


HttpListener.AuthenticationSchemes 屬性

定義

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

C#
public System.Net.AuthenticationSchemes AuthenticationSchemes { get; set; }

屬性值

AuthenticationSchemes 列舉值的位元組合,表示用戶端的驗證方式。 預設值是 Anonymous

例外狀況

此物件已關閉。

範例

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

C#
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();
}

備註

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

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

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

注意

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

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1