簡體   English   中英

無法使用 Poweshell 的私有證書連接到 Exchange Online

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

我正在嘗試使用下一個流程創建 powershell 腳本。

  1. 通過應用程序登錄到 Azure Active Directory。
  2. 創建私有證書。
  3. 將證書上傳到 Azure AD 應用證書。
  4. 連接到 ExchangeOnline。

為此,我根據以下步驟創建了下一個腳本: 第一步:

$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

第二和 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

到目前為止,一切都很好。 我可以在應用程序的證書列表中看到此證書。 但是,當我要使用此命令連接到 MS Exchange Online 時:

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

我得到下一個問題:

{
   "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"
}

但是這個新創建的證書不能過期。 我很高興看到任何想法。 與此堆疊了幾天。

編輯:我也承認,如果我用 UI 上傳新創建的證書而不是用這個命令:

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

然后我可以用新創建的證書在線交換

問題在於$validTo = $validTo.AddDays(-1); . 因此, $validTo早於$cert.NotAfter

請像這樣修改腳本:

$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