azure - 当 sp 是在没有 App 和 Cred 的情况下独立创建时,如何获取与 AzureAD 中的服务主体关联的证书的指纹

标签 azure powershell azure-active-directory

我有一个使用下面的 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 → 应用程序注册 → → Manifest 下创建的服务主体下的 list ,但 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

例如

  1. 创建 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

enter image description here

enter image description here

关于azure - 当 sp 是在没有 App 和 Cred 的情况下独立创建时,如何获取与 AzureAD 中的服务主体关联的证书的指纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068055/

相关文章:

azure - 为通过 Azure 函数输出绑定(bind)添加到 Azure 队列的消息设置 VisibilityTimeout

c# - Microsoft.Azure.Management.Conspiration .NET 包和 ApiVersion

azure - Azure上的VM如何将公共(public)端口80映射到私有(private)端口8080

azure - 如何在azure api管理中使用订阅 key (主要/次要)获取用户详细信息?

azure - 为什么我的 Azure 网站不接受 OAuth token ?

azure - 失败的测试不会使 YAML 中的任务失败

windows - 如何从powershell或cmd调出windows 10共享对话框

powershell - 如何使用 Powershell 重命名文件夹以将年份放在首位

powershell - 如何比较字符串末尾的数字值?

azure - PowerShell 版本及其管理 Azure Active Directory 的能力有何区别?