c# - 如何使用证书连接-Exchange Online?
问题描述
我正在使用证书进行 Connect-Exchange Online。为此,我有 2 种方法来连接 Connect-ExchangeOnline 和所有涉及我必须在本地计算机上安装证书或必须使用直接证书路径的文档。但是我们不能将证书保存在我们的服务器上,所以如何提取证书并将其用于我们的应用程序。
那么有什么方法可以使用直接证书而不安装在本地机器上,只需从 pfx 证书中提取数据并使用它来连接在线交换?
我尝试了以下方法。
- 有证书
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();
}
- 带证书路径
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# - 读取现有的 xml 文件并应用更改
- node.js - 如何在 mocha 中编写测试用例以获得启用的 Web 套接字的响应?
- apache-spark - 读取自定义 pyspark 变压器
- javascript - 使用 AJAX 更改 Twig 模板变量
- ms-access - 从可填写的 PDF 数据导入 MS Access vba
- php - PHP curl 和输出 api 响应
- javascript - 从可点击列表中获取 ID
- android - 启动 InstallReferrerService (TagManager) 时出现 IllegalStateException
- android - 不使用主题中声明的颜色重音的材质组件
- sql-server - 带有 SET 和 LIKE 的 Sql 查询显示不正确的结果