微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在开发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;

原文地址:https://www.jb51.cc/csharp/243562.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐