• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
专注于技术,专注于生活
这样才是程序员的生命
博客园    首页    新随笔    联系   管理    订阅  订阅

c# 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系

调用https出差,提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系"。 

定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一个类,然后在客户单调用WCF服务之前,执行一次即可。代码如下:
using System.Net;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

/// <summary>
    /// https访问设置证书问题
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// 设置证书安全性
        /// </summary>
        public static void SetCertificatePolicy()
        {
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
        }

        /// <summary>
        /// 远程证书验证
        /// </summary>
        private static bool RemoteCertificateValidate(
           object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            // trust any certificate!!!
            //System.Console.WriteLine("Warning, trust any certificate");
            return true;
        }
    }

在WebRequest.Create("https://……").GetResponse();调用操作点前先调用这个方法: Util.SetCertificatePolicy();

posted @ 2022-05-07 19:54  錒飛  阅读(1505)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3