前言
在我们使用.NET 开发应用时,通常会调用一些API接口,而这些接口为了数据安全或者被滥用等安全威胁,都会启用SSL/TLS。一般的调用地址会以https开头。
1、什么是网络安全协议
网络安全协议是用于保护计算机网络中的数据传输和通信过程的安全机制,它定义了数据在网络中传输的方式、数据加密和解密的算法,以及身份验证和访问控制的规则
网络安全协议主要目标是在不可信的网络环境中建立起安全的通信渠道,以防止未经授权的方问、数据篡改和信息泄露等安全威胁。
常见的网络安全协议有SSL/TLS、IPsec 、SSH、WPA/WPA2、DNSSEC、S/MIME、SSL VPN等,本文介绍.NET 是支持TLS安全协议的及显式指定协议版本。
2、.NET 对传输层安全协议TLS的支持
.NET 4.0 及以上版本都默认提供支持的TLS版本,但.NET Framework 3.5.1 及更早版本不支持应用程序使用传输层安全(TLS)系统默认版本作为加密协议。
3、.NET 低版本如何支持TLS1.2
文章主要介绍用代码来更改,使应用程序支持TLS1.2 或指定版本。ServicePointManager类就是一个管理HTTP管理器的库,通过设SecurityProtocol 属性来指定TLS的版本。这样可以解决应用程序由于版本不支持导致请求通信出现如不支持请求的安全协议、基础连接已经关闭: 发送时发生错误、请求被中止: 未能创建 SSL/TLS 安全通道等问题。下面我们通过一段代码了解:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Web;
namespace Fountain.Net.Core.WebRequestClient
{
public class WebRequest
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
/// <summary>
/// POS请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="parameters">参数名称与参数值</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static string Post(string _Porturl, IDictionary<string, string> parameters, int? _Timeout, CookieCollection _Cookies)
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
ASCIIEncoding requestEncoding = new ASCIIEncoding();
//垃圾回收
System.GC.Collect();
ServicePointManager.DefaultConnectionLimit = 512;
if (_Porturl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
// 指定TLS的版本 0x30-Ssl3、0xc0-Tls、0x300-Tls11、0xc00-Tls12、3072-Tls1.2【Windows 7】 768-Tls1.1【Windows 7】
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00) | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.Expect100Continue = false;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
httpWebRequest = WebRequest.Create(_Porturl) as HttpWebRequest;
httpWebRequest.ProtocolVersion = HttpVersion.Version10;
}
else
{
//HTTP请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(_Porturl);
}
httpWebRequest.KeepAlive = false;
//设置请求方法
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//User-Agent HTTP标头值
httpWebRequest.UserAgent = DefaultUserAgent;
//设置请求超时
if (_Timeout.HasValue)
{
httpWebRequest.Timeout = _Timeout.Value;
}
if (_Cookies != null)
{
httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.CookieContainer.Add(_Cookies);
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder stringBuffer = new StringBuilder();
foreach (string key in parameters.Keys)
{
if (stringBuffer.Length > 0)
{
stringBuffer.AppendFormat("&{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(parameters[key]));
}
else
{
stringBuffer.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(parameters[key]));
}
}
byte[] sendContent = requestEncoding.GetBytes(stringBuffer.ToString());
httpWebRequest.ContentLength = sendContent.Length;
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(sendContent, 0, sendContent.Length);
}
}
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream getStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(getStream);
return streamReader.ReadToEnd();
}
catch (Exception objException)
{
if (objException.InnerException != null)
{
throw new Exception(objException.InnerException.Message);
}
throw new Exception(objException.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="chain"></param>
/// <param name="errosslPolicyErrorsrs"></param>
/// <returns></returns>
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
return false;
}
}
}
为了确保.NET Framework 应用程序保持安全,TLS版本不应进行硬编码。.NET Framework 应用程序应使用操作系统 (OS) 支持的 TLS 版本。
小结
ServicePointManager.SecurityProtocol 属性是.NET 中用于设置网络安全协议的重要属性。希望通过本文对你了解网络安全协议、解决开发过程出现的问题有帮助。