【问题标题】:How to get thumbprint of the cert associated with a service principal in AzureAD when the sp is created independently without App and Cred当 sp 是在没有 App 和 Cred 的情况下独立创建时,如何获取与 AzureAD 中的服务主体关联的证书的指纹
【发布时间】:2020-02-05 02:04:24
【问题描述】:

我有一个使用以下 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。此服务主体工作正常,我可以在使用证书时获得令牌。

在 Azure AD 中创建服务主体后,如何使用 Powershell 查看与服务主体关联的证书指纹?

我尝试过this,但是当我尝试执行 Get-AzureADApplicationKeyCredential 时得到Forbidden

我还在 Azure Active Directory 下创建的服务主体下检查了 Azure 门户中的清单 → 应用注册 → → 清单,但 keyCredentials 节点为空

"keyCredentials": [],

请注意,当我使用New-AzureRmADApplication 后跟凭据New-AzureRmADAppCredentialNew-AzureRmADServicePrincipal 创建应用程序时,我会看到keyCredentialscustomKeyIdentifier 设置为证书指纹。下面的示例脚本 -

$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 中的服务主体关联的证书的指纹?

【问题讨论】:

  • 哦,这很有趣。您是否尝试过通过例如检查 SP 对象?图形资源管理器? Https://aka.ms/ge
  • @juunas,是的,虽然我无法让 Graph Explorer 工作。当我调用它时,我总是得到一个空的应用程序列表。所以我无法进一步挖掘特定的应用程序。 ://
  • 您是否尝试获取 /servicePrincipals/the-objectId-from-first-command?我的意思是 PowerShell 命令应该告诉你 id
  • 是的,https://graph.microsoft.com/beta/serviceprincipals/ 也给了我空列表:(。我可以看到信息(或没有信息)的唯一地方是在清单页面上。

标签: azure powershell azure-active-directory


【解决方案1】:

根据我的测试,我们可以使用下面的 Azure AD Graph API 来获取 sp.key 的凭证。 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

【讨论】:

  • 谢谢,我在$context.TokenCache 中没有令牌,其中$_.Resource -eq "https://graph.windows.net/"。我错过了什么吗?
  • 没关系,在执行$ctx=Get-AzureRmContextGet-AzureRmADUser -UserPrincipalName $ctx.Account.Id 后,我能够在我的令牌缓存中获得https://graph.windows.net/ 资源的令牌,如here 所述。谢谢!
【解决方案2】:

我测试了你的命令,它应该可以工作。使用New-AzADServicePrincipal创建服务主体时,会自动为你创建一个AD App(即App Registration),证书也会出现在你的AD App的Certificates &amp; secrets中。

在我的示例中,我使用了新的Az 模块,对于您使用的旧的AzureRm 模块,它应该也可以工作(不完全确定,我建议您使用新的Az 模块,因为AzureRm 模块已被弃用,不会更新)。并确保您在门户中查看正确的 AD 应用程序,因为 AD 应用程序的 DisplayName 可能会重复。

$cert=New-SelfSignedCertificate -Subject "CN=TodoListDaemonWithCert" -CertStoreLocation "Cert:\CurrentUser\My"  -KeyExportPolicy Exportable -KeySpec Signature
$bin = $cert.RawData
$base64Value = [System.Convert]::ToBase64String($bin)

New-AzADServicePrincipal -DisplayName joy134 -CertValue $base64Value 

检查门户:

然后您可以使用您尝试过的this way 来修复Forbidden 错误,您的帐户至少应该是AD App 的Owner,或者如果您的帐户在租户中具有管理员角色,例如Application administratorGroups administrator,也可以。

$CustomKeyIdentifier = (Get-AzureADApplicationKeyCredential -ObjectId "<object-id>").CustomKeyIdentifier
$Thumbprint = [System.Convert]::ToBase64String($CustomKeyIdentifier)


此外,您应该注意不同的命令组合会导致不同的结果,请参阅link。所以当你测试它时,我建议你使用不同的参数值。

【讨论】:

  • 谢谢,我需要一种方法来处理已经使用AzureRm 模块创建的服务原则
  • 按照您的脚本,如果应用程序拥有两个以上的证书怎么办。 $Thumbprint 在单行中返回指纹并且不能为每个指纹做。 $CustomKeyIdentifier = (Get-AzureADApplicationKeyCredential -ObjectId "xxxx19-4xxx-a759-a2xxxxxx4504").CustomKeyIdentifier $Thumbprint = [System.Convert]::ToBase64String($CustomKeyIdentifier)
猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2017-05-01
  • 2020-04-22
相关资源
最近更新 更多