简体   繁体   English

无法使用 Poweshell 的私有证书连接到 Exchange Online

[英]Can't Connect to Exchange Online with a private certificate at Poweshell

I'm trying to create powershell script with the next flow.我正在尝试使用下一个流程创建 powershell 脚本。

  1. Login to Azure Active Directory via Application.通过应用程序登录到 Azure Active Directory。
  2. Create Private Certificate.创建私有证书。
  3. Upload Certificate to Azure AD Application Certificates.将证书上传到 Azure AD 应用证书。
  4. Connect to ExchangeOnline.连接到 ExchangeOnline。

For this I created the next sсript according to the steps: 1st Step:为此,我根据以下步骤创建了下一个脚本: 第一步:

$clientId = 'xxx'
$tenantId = 'xxx'
$clientSecret = 'xxx'
$org = 'xxx.onmicrosoft.com'
$clientSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$credObject = New-Object System.Management.Automation.PSCredential ($clientId, $clientSecret)
Connect-AzAccount -Credential $credObject -Tenant $tenantId -ServicePrincipal

2nd and 3d Step:第二和 3d 步骤:

$cert = New-SelfSignedCertificate -DnsName $org -NotAfter (Get-Date).AddYears(1) -KeySpec KeyExchange
$binCert = $cert.GetRawCertData()
$credValue = [System.Convert]::ToBase64String($binCert)
$validFrom = [datetime]::Parse($cert.GetEffectiveDateString())
$validTo = [datetime]::Parse($pfx.GetExpirationDateString())
$validTo = $validTo.AddDays(-1);
New-AzADAppCredential -ApplicationId $clientId -CertValue $credValue -StartDate $validFrom -EndDate $validTo

And up to now all is going fine.到目前为止,一切都很好。 I can see this certificate at certificates list of Application.我可以在应用程序的证书列表中看到此证书。 But when I'm going to connect to MS Exchange Online with this command:但是,当我要使用此命令连接到 MS Exchange Online 时:

Connect-ExchangeOnline -Certificate $cert -AppID $clientId -Organization $org

i getting the next issue:我得到下一个问题:

{
   "error":"invalid_client",
   "error_description":"xxx: Client assertion contains an invalid signature. [Reason - The key used is expired., Thumbprint of key used by client: 'xxx', Found key 'Start=03/11/2021 14:59:26, End=03/11/2022 13:09:26', Please visit the Azure Portal, Graph Explorer or directly use MS Graph to see configured keys for app Id 'xxx'. Review the documentation at https://docs.microsoft.com/en-us/graph/deployments to determine the corresponding service endpoint and https://docs.microsoft.com/en-us/graph/api/application-get?view=graph-rest-1.0&tabs=http to build a query request URL, such as 'https://graph.microsoft.com/beta/applications/xxx']\r\nTrace ID: xxxx\r\nCorrelation ID: xxx\r\nTimestamp: 2021-03-11 13:15:28Z",
   "error_codes":[
      700027
   ],
   "timestamp":"2021-03-11 13:15:28Z",
   "trace_id":"xxx",
   "correlation_id":"xxx",
   "error_uri":"https://login.microsoftonline.com/error?code=700027"
}

But this newly created certificate could not expire.但是这个新创建的证书不能过期。 I'll be glad to see any idea.我很高兴看到任何想法。 Stacked with this for few days.与此堆叠了几天。

EDIT: Also i admitted that if i uploading newly created certificate with UI but not with this command:编辑:我也承认,如果我用 UI 上传新创建的证书而不是用这个命令:

New-AzADAppCredential -ApplicationId $clientId -CertValue $credValue -StartDate $validFrom -EndDate $validTo

Then i can to exchange online with newly created certificate然后我可以用新创建的证书在线交换

The issue lies on $validTo = $validTo.AddDays(-1);问题在于$validTo = $validTo.AddDays(-1); . . As a result, $validTo is earlier than $cert.NotAfter .因此, $validTo早于$cert.NotAfter

Please modify the script like this:请像这样修改脚本:

$cert = New-SelfSignedCertificate -DnsName $org -CertStoreLocation "cert:\LocalMachine\My" -NotAfter (Get-Date).AddYears(1) -KeySpec KeyExchange
$binCert = $cert.GetRawCertData()
$credValue = [System.Convert]::ToBase64String($binCert)
New-AzADAppCredential -ApplicationId $clientId -CertValue $credValue -StartDate $cert.NotBefore -EndDate $cert.NotAfter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM