WebRequest.AuthenticationLevel 屬性

定義

取得或設定值,指出用於這個要求的驗證和模擬等級。

C#
public System.Net.Security.AuthenticationLevel AuthenticationLevel { get; set; }

屬性值

AuthenticationLevel 值的位元組合。 預設值是 MutualAuthRequested

在相互驗證中,用戶端和伺服器都會出示建立其識別的認證。 MutualAuthRequiredMutualAuthRequested 與 Kerberos 驗證有關。 Kerberos 驗證可以直接支援,或在使用交涉安全性通訊協定時,用來選取實際的安全性通訊協定。 如需驗證通訊協議的詳細資訊,請參閱 因特網驗證

若要判斷是否發生相互驗證現象,請檢查 IsMutuallyAuthenticated 屬性。

如果您指定 MutualAuthRequired 驗證旗標值,但相互驗證未發生,則應用程式會收到 IOException 以及 ProtocolViolationException 內部例外狀況,指出相互驗證失敗。

範例

下列程式代碼範例會設定此屬性的值。

C#

// The following example uses the System, System.Net,
// and System.IO namespaces.

public static void RequestMutualAuth(Uri resource)
{
    // Create a new HttpWebRequest object for the specified resource.
    WebRequest request=(WebRequest) WebRequest.Create(resource);
    // Request mutual authentication.
   request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
    // Supply client credentials.
    request.Credentials = CredentialCache.DefaultCredentials;
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    // Determine whether mutual authentication was used.
    Console.WriteLine("Is mutually authenticated? {0}", response.IsMutuallyAuthenticated);
    // Read and display the response.
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
   Console.WriteLine(responseString);
    // Close the stream objects.
    streamResponse.Close();
    streamRead.Close();
    // Release the HttpWebResponse.
    response.Close();
}

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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