.NET 与网络安全协议SSL/TLS建立信任的通信关系

前言

图片

       在我们使用.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 中用于设置网络安全协议的重要属性。希望通过本文对你了解网络安全协议、解决开发过程出现的问题有帮助。

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Netty是一款高性能的网络应用框架,支持SSL/TLS加密来保护网络通信安全性。SSL(Secure Sockets Layer)和TLS(Transport Layer Security)是网络通信中广泛使用的加密协议,用于在客户端和服务器之间建立安全通信信道。 Netty提供了一些组件和类来实现SSL/TLS加密。首先,我们需要使用javax.net.ssl包中的类来创建SSLContext对象。SSLContext是SSL/TLS协议的入口点,它包含用于加密和解密数据的加密算法和密钥。我们需要为SSLContext对象配置密钥库和信任库,密钥库用于存储证书和私钥,而信任库用于存储可信的证书。 接下来,我们需要创建SslHandler对象,将其添加到Netty的ChannelPipeline中。SslHandler作为一个ChannelHandler,负责处理SSL/TLS握手过程和数据的加密解密。当建立连接时,SslHandler会自动执行握手过程,包括协商加密算法、验证证书以及生成会话密钥等。 一旦握手完成,SslHandler会将数据加密后发送到网络,并将接收到的密文解密成明文。这样可以确保在网络传输过程中的数据保密性和完整性。此外,SslHandler还提供了一些方法来获取会话信息,如远程主机的证书和协商的加密算法。 使用Netty的SSL/TLS加密功能能够有效地提高网络通信安全性。通过配置SSLContext和添加SslHandler,我们可以方便地实现对网络通信的加密和解密。无论是在客户端还是服务器端,都可以使用Netty的SSL/TLS加密功能来保护数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fountyuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值