我有一个使用下面的 powershell 创建的服务主体。
$sp3 = New-AzureRmADServicePrincipal `
-DisplayName "<service-principal-name>" `
-CertValue $certValue3 `
-EndDate ([System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($cert3.Certificate.GetExpirationDateString(), [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time'))
其中 certValue3
是 Base64String RawCertData。该服务主体工作正常,我可以在使用证书时获得 token 。
在 Azure AD 中创建服务主体后,如何使用 Powershell 查看与服务主体关联的证书的指纹?
我已经尝试过this ,但当我尝试执行 Get-AzureADApplicationKeyCredential
时,我收到Forbidden
我还检查了 Azure 门户中在 Azure Active Directory → 应用程序注册 → keyCredentials
节点为空
"keyCredentials": [],
请注意,当我使用 New-AzureRmADApplication
创建应用程序,然后使用凭据 New-AzureRmADAppCredential
,然后使用 New-AzureRmADServicePrincipal
,然后我请参阅将 customKeyIdentifier
设置为证书指纹的 keyCredentials
。下面的示例脚本 -
$adapp = New-AzureRmADApplication -DisplayName "<application-name>" `
-HomePage "<home-page-url>" `
-IdentifierUris "<identifier-url>" `
-CertValue $certValue `
-StartDate ([System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($cert.Certificate.GetEffectiveDateString(), [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time')) `
-EndDate ([System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($cert.Certificate.GetExpirationDateString(), [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time'))
New-AzureRmADAppCredential -ApplicationId $adapp.ApplicationId -CertValue $certValue2
$sp2 = New-AzureRmADServicePrincipal -ApplicationId $adapp.ApplicationId -DisplayName "<application-name>"
当服务主体是在没有 AzureRmADApplication 和 AzureRmADAppCredential 的情况下独立创建时,如何使用 powershell 获取与 Azure AD 中的服务主体关联的证书的指纹?
最佳答案
根据我的测试,我们可以使用以下Azure AD Graph API来获取sp的关键凭据。 KeyCredential中的customKeyIdentifier是证书的指纹
GET https://graph.windows.net/<your teanant id>/servicePrincipals/<your sp object id>/keyCredentials?api-version=1.6
例如
- 创建 sp 并获取指纹
$tenantId ="<tenant id>"
#use the goabl admin account to login
Connect-AzureRmAccount -Tenant $tenantId
$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificateObject.Import("E:\Cert\examplecert.pfx","Password0123!", [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)
Write-Host "the thumbrint of cert"
$certificateObject.Thumbprint
$keyValue = [System.Convert]::ToBase64String($certificateObject.GetRawCertData())
$sp =New-AzureRmADServicePrincipal -DisplayName "jimtestsample" -CertValue $keyValue -EndDate $endDate
$context=Get-AzureRmContext
$token=$context.TokenCache.ReadItems() |Where-Object { ($_.TenantId -eq $tenantId) -and ($_.Resource -eq "https://graph.windows.net/") }
$accesstoken=$token.AccessToken
$url = "https://graph.windows.net/$tenantId/servicePrincipals/"+$sp.Id+"/keyCredentials?api-version=1.6"
$keyCreds = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization" = "Bearer $accesstoken"}
Write-Host "--------------------------------------------"
$keyCreds.value | Select-Object customKeyIdentifier
关于azure - 当 sp 是在没有 App 和 Cred 的情况下独立创建时,如何获取与 AzureAD 中的服务主体关联的证书的指纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068055/