加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何忽略SSL证书是由未知的证书颁发机构问题签名的?

发布时间:2020-12-15 08:26:13 所属栏目:百科 来源:网络整理
导读:我正在开发c#应用程序来调用Exchange Management Shell Cmdlet.它总是出现“目标计算机上的服务器证书(208.243.XX.2XX:443)有以下错误: SSL证书由未知的证书颁发机构签名. SSL证书包含与主机名不匹配的公用名(CN). “ 但是我确实编写了接受所有证书的代码,
我正在开发c#应用程序来调用Exchange Management Shell Cmdlet.它总是出现“目标计算机上的服务器证书(208.243.XX.2XX:443)有以下错误:
SSL证书由未知的证书颁发机构签名.
SSL证书包含与主机名不匹配的公用名(CN). “

但是我确实编写了接受所有证书的代码,不知道为什么还会得到错误.

我的代码:

PSCredential credential = new PSCredential("administrator",securePwd);

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://208.243.49.20/powershell"),"http://schemas.microsoft.com/powershell/Microsoft.Exchange",credential);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    command.AddCommand("New-Mailbox");
    command.AddParameter("Name","TestName");
    powershell.Commands = command;
    ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
    delegate { return true; }
);
    try
    {
        runspace.Open();//This is where the exception happens
        powershell.Runspace = runspace;
        Collection<PSObject> result= powershell.Invoke();
    }

解决方法

WSManConnectionInfo对象有两个属性可以跳过证书检查.
connectionInfo.SkipCACheck = true;

connectionInfo.SkipCNCheck = true;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读