我正在开发C#应用程序调用Exchange管理Shell Cmdlets。然而,总是出现以下异常:"目标计算机(208.243.XX.2XX:443)上的服务器证书具有以下错误:
SSL证书由未知证书颁发机构签名。
SSL证书包含不匹配主机名的通用名称(CN)。"
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();
}