」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何確定.NET中的談判TLS版本?

如何確定.NET中的談判TLS版本?

發佈於2025-03-22
瀏覽:257

[2

在.NET應用程序中確定協商的TLS版本How Can I Determine the Negotiated TLS Version in .NET?
此技術利用反思訪問內部屬性和字段以獲取SSL協議版本。 請注意,這取決於內部API,並且可能會隨後的.NET更新而發生變化。

使用system.io.compression; 使用system.net; 使用system.net.security; 使用System.Defrection; 使用system.security.authentication; 使用System.Security.Cryptography; 使用system.security.cryptography.x509certificates; // ...其他代碼... ServicePointManager.SecurityProtocol = SecurityProtocoColType.tls | SecurityProtocolType.tls11 | SecurityProtocolType.tls12 | SecurityProtocolType.tls13; // ...其他代碼... uri requesturi = new Uri(“ https://somesite.com”); var request = webrequest.createhttp(requesturi); // ...其他代碼... 使用(var requestStream = request.getRequestStream()){ //請求流驗證;現在提取SSL協議 SSLProtocols sslProtocol = ExtractssslProtocol(requestStream); 如果(sslprotocol!= sslprotocols.none){ //處理SSLProtocol值 } } // ... ExtractsslProtocol函數(將在此處提供實現)...

方法2:安全連接上下文屬性(高級)

此方法通過

ecure32.dll

庫訪問連接上下文屬性。 這種方法涉及使用非公共手柄和結構,從而使其便於攜帶,並可能更加複雜。 (由於復雜性和潛在的不穩定而省略了詳細的實現。)

[2 。網絡版本:

TLS 1.3支持需要.net Framework 4.8或更高版本,或.NET CORE 3.0。

[remoteCertificateValidationCallback

using System.IO.Compression;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

// ... other code ...

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 
                                       SecurityProtocolType.Tls11 | 
                                       SecurityProtocolType.Tls12 | 
                                       SecurityProtocolType.Tls13;

// ... other code ...

Uri requestUri = new Uri("https://somesite.com");
var request = WebRequest.CreateHttp(requestUri);

// ... other code ...

using (var requestStream = request.GetRequestStream()) {
    // Request stream validated; now extract SSL protocol
    SslProtocols sslProtocol = ExtractSslProtocol(requestStream);
    if (sslProtocol != SslProtocols.None) {
        // Process the sslProtocol value
    }
}

// ... ExtractSslProtocol function (implementation would be provided here) ...
TcpClient

: Using TcpClient

allows retrieving TLS information

before WebRequest

initialization, enabling proactive TLS version determination.

此信息可幫助開發人員了解和管理其.NET應用程序使用的安全協議。 請記住要仔細考慮與使用反射和與不受管理的庫相互作用相關的含義和潛在風險。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3