我正在開發c#應用程序來調用Exchange命令行管理程序Cmdlet。它總是會出現一個例外:「目標計算機上的服務器證書(208.243.XX.2XX:443)出現以下錯誤:
SSL證書由未知證書頒發機構簽署。
SSL證書包含通用名稱(CN)與主機名稱不匹配。「如何忽略SSL證書由未知的證書頒發機構問題簽名?
但我確實編寫了接受所有證書的代碼,不知道爲什麼還是得到錯誤。
我的代碼:
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();
}
感謝您的快速回復。在創建運行空間實例之前,我移動了ServicePointManager,發生了相同的錯誤。我改變返回true返回false,同樣的錯誤。我不確定是否從單獨的進程執行powershell,但似乎ServicePointManager在此過程中沒有任何關係。如果它是一個獨立的過程,那該怎麼辦?提前致謝。 –
你有沒有試過把它作爲第一個命令? –
我越來越相信這是因爲powershell運行在一個單獨的過程中。您需要讓powershell腳本執行與ServicePointManager配置步驟等效的操作。 –