CA5364:請勿使用已取代的安全性通訊協定

屬性
規則識別碼 CA5364
標題 請勿使用已取代的安全性通訊協定
類別 安全性
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

當符合下列任一條件時,就會引發此規則:

已被取代的值包括:

  • Ssl3
  • Tls
  • Tls10
  • Tls11

檔案描述

傳輸層安全性 (TLS) 最常使用超文字傳輸通訊協定安全 (HTTPS) 保護電腦之間的通訊。 較舊的 TLS 通訊協定版本的安全性比 TLS 1.2 和 TLS 1.3 更低,而且較可能有新的弱點。 避免舊版通訊協定,以將風險降到最低。 如需識別和移除已取代的通訊協定版本的指引,請參閱 解決 TLS 1.0 問題,第 2 版

如何修正違規

請勿使用已被取代的 TLS 通訊協定版本。

隱藏警告的時機

如果下列狀況,您可以隱藏此警告:

  • 已淘汰通訊協定版本的參考不會用來啟用已被取代的版本。
  • 您必須連線到無法升級為使用安全 TLS 組態的舊版服務。

隱藏警告

如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable CA5364
// The code that's violating the rule is on this line.
#pragma warning restore CA5364

若要停用檔案、資料夾或項目的規則,請在組態檔中將其嚴重性設定為 。none

[*.{cs,vb}]
dotnet_diagnostic.CA5364.severity = none

如需詳細資訊,請參閱 如何隱藏程式代碼分析警告

虛擬程式代碼範例

列舉名稱違規

using System;
using System.Net;

public class ExampleClass
{
    public void ExampleMethod()
    {
        // CA5364 violation
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' CA5364 violation
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
    End Sub
End Class

整數值違規

using System;
using System.Net;

public class ExampleClass
{
    public void ExampleMethod()
    {
        // CA5364 violation
        ServicePointManager.SecurityProtocol = (SecurityProtocolType) 768;    // TLS 1.1
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' CA5364 violation
        ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType)   ' TLS 1.1
    End Sub
End Class

解決方案

using System;
using System.Net;

public class TestClass
{
    public void TestMethod()
    {
        // Let the operating system decide what TLS protocol version to use.
        // See https://learn.microsoft.com/dotnet/framework/network-programming/tls
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' Let the operating system decide what TLS protocol version to use.
        ' See https://learn.microsoft.com/dotnet/framework/network-programming/tls
    End Sub
End Class

CA5386:避免將 SecurityProtocolType 值寫入程式碼

CA5397:請勿使用已過時的 SslProtocols 通訊協定

CA5398:避免以硬式編碼方式寫入 SslProtocols 值