首页 > 解决方案 > 如何使用证书连接-Exchange Online?

问题描述

我正在使用证书进行 Connect-Exchange Online。为此,我有 2 种方法来连接 Connect-ExchangeOnline 和所有涉及我必须在本地计算机上安装证书或必须使用直接证书路径的文档。但是我们不能将证书保存在我们的服务器上,所以如何提取证书并将其用于我们的应用程序。

那么有什么方法可以使用直接证书而不安装在本地机器上,只需从 pfx 证书中提取数据并使用它来连接在线交换?

我尝试了以下方法。

  1. 有证书
public static void connect_04_withCertificate()
            {
                string path = "\"D:\\MEM\\cert\\cert\\NewCetificate.pfx\";";
                string password = "\"12345\";";
                string id = "\"ABCD_ABCD_ABCD_ABCD_ABCD_ABCD\"";
                string org = "\"Anup.Gupta.com\"";
   
                string cmd4 = "$CertificateFilePath = " + path + " $CertificateFilePassword = " + password;
               
                string cmd5 = " $certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertificateFilePath,$CertificateFilePassword);";
               
                string cmd6 = "echo $certificateObject;";
               
                string cmd9 = "Connect-ExchangeOnline -Certificate $certificateObject -AppID " + id + " -Organization " + org + ";";
                 string cmd10 = "Get-Mailbox";
   
                string cmd2 = cmd4 + cmd5 + cmd6 + cmd9 + cmd10;
                Console.Write(cmd2);
    
                var powerShell = PowerShell.Create().AddScript(cmd2);
                // begin invoke execution on the pipeline
                IAsyncResult asyncpl = powerShell.BeginInvoke();
               
                foreach (PSObject result in powerShell.EndInvoke(asyncpl))
                {
                   
                     Console.WriteLine(result);
                } // End foreach.
    
                Console.Read();
            }
  1. 带证书路径
public static void connect_05_withCertificate(){

            string path = "\"D:\\MEM\\cert\\cert\\NewCetificate.pfx\"";
            string password = "\"12345\"";
            string appId = "\"ABCD_ABCD_ABCD_ABCD_ABCD_ABCD\"";
            string org = "\"Anup.Gupta.com\"";

            string cmd4 = "$CertificateFilePath = " + path + " $CertificateFilePassword = " + password;
          
            string CertificatePassword = "-CertificatePassword(ConvertTo-SecureString -String "+ password + " -AsPlainText -Force)";
            string PrintCertificatePassword = "echo -CertificatePassword(ConvertTo-SecureString -String " + password + " -AsPlainText -Force);";

            string cmd9 = "Connect-ExchangeOnline -CertificateFilePath "+ path + CertificatePassword + " -AppID " + appId + " -Organization " + org + ";";
            string cmd10 = "Get-Mailbox";

            string cmd2 = PrintCertificatePassword + cmd9 + cmd10;
            Console.Write(cmd2);
            
            var powerShell = PowerShell.Create().AddScript(cmd2);
            // begin invoke execution on the pipeline
            IAsyncResult asyncpl = powerShell.BeginInvoke();

            foreach (PSObject result in powerShell.EndInvoke(asyncpl))
            {
                
                Console.WriteLine(result);
            }
            Console.Read();
        }

标签: c#.netssl.net-coreexchange-server

解决方案


推荐阅读