Forum Discussion

kscisd's avatar
kscisd
Copper Contributor
Apr 08, 2022

Connecting to Exchange Online with a Certificate and C#

Anyone using c# to connect to Exchange online? I am running into a problem that I can't seem to make progress on.

 

I have the following snippet of code trying to connect to exchange online:

public Runspace getSpace() {
    String schema = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
    Uri server = new Uri("https://outlook.office365.com/PowerShell");
    string certificateThumbprint = "thumbprint";

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(server, schema, certificateThumbprint);
    Runspace rsp = RunspaceFactory.CreateRunspace(connectionInfo );
    rsp.Open();
    return rsp;
}

Which results in:

System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server outlook.office365.com failed with the following error message :  For more information, see the about_Remote_Troubleshooting Help topic.
   at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()

I know the cert is working because when I do the following I am able to connect:

Connect-ExchangeOnline -AppId "application guid" -Organization "tenent.onmicrosoft.com" -CertificateThumbprint "thumbprint"

Any ideas on what I could try next? Thanks!

Resources