【问题标题】:Create a Application in Azure AD with Azure PowerShell Certificate authentication使用 Azure PowerShell 证书身份验证在 Azure AD 中创建应用程序
【发布时间】:2016-06-08 23:33:36
【问题描述】:

我尝试使用 Azure PowerShell 证书身份验证在 Azure AD 中创建应用程序,下面是 Powershell sn-p:

Login-AzureRmAccount
$cert = New-Object    System.Security.Cryptography.X509Certificates.X509Certificate("PATH_TO_CER_FILE")
$key = [System.Convert]::ToBase64String($cert.GetRawCertData())
$app = New-AzureRmADApplication -DisplayName "SetupTet4" -HomePage  "http://localhost" -IdentifierUris "http://localhost" -KeyValue $key -KeyType AsymmetricX509Cert
New-AzureRmADServicePrincipal -ApplicationId $app.ApplicationId
New-AzureRmRoleAssignment -RoleDefinitionName "Owner" -ServicePrincipalName  $app.ApplicationId 

Azure AD 应用程序已成功创建,但是对于具有证书身份验证的 Azure AD 应用程序,customKeyIdentifierkeyCredentials 中的值在创建后为 null,这是我从 Azure 下载的应用程序清单的一部分传送门:

"keyCredentials": [{
"customKeyIdentifier": null,
"endDate": "2017-02-25T20:48:35.5174541Z",
"keyId": "575580cc-ce4e-4862-ad3e-1ba5833fe7f6",
"startDate": "2016-02-25T20:48:35.5174541Z",
"type": "AsymmetricX509Cert",
"usage": "Verify",
"value": null
}],

仅供参考,证书是我使用本地生成的 makecert 命令的自签名证书。

任何建议,非常感谢。

詹姆斯

【问题讨论】:

  • 有什么问题?您发布的步骤是在 Azure AD 中创建应用程序的步骤。您对 customKeyIdentifier 和 value 为 null 有什么顾虑?
  • Rick,谢谢你的提问,这就是问题所在,如果 customKeyIdentifier 和 value 为 null,App Authentication 将失败。
  • 我也尝试了 Graph API,请遵循以下代码:[link] (github.com/Azure-Samples/…) 但结果相同,customKeyIdentifier 和 value 为 null,这是 MS API 中的错误还是我错过了什么?
  • 我刚刚注意到您缺少什么 - “Set-AzureRmKeyVaultAccessPolicy”。我部署了相同的环境。我刚刚检查了我的脚本,发现你的脚本中没有这个。我会在几分钟后将其作为解决方案发布,以便您查看全部内容。

标签: azure certificate azure-active-directory azure-powershell azure-keyvault


【解决方案1】:

添加对 Set-AzureRmKeyVaultAccessPolicy 的调用以指定您希望服务原则对密钥保管库具有的访问级别。查看脚本最后两行的更改。

Login-AzureRmAccount
$cert = New-Object    System.Security.Cryptography.X509Certificates.X509Certificate("PATH_TO_CER_FILE")
$key = [System.Convert]::ToBase64String($cert.GetRawCertData())
$app = New-AzureRmADApplication -DisplayName "SetupTet4" -HomePage  "http://localhost" -IdentifierUris "http://localhost" -KeyValue $key -KeyType AsymmetricX509Cert
$sp = New-AzureRmADServicePrincipal -ApplicationId $app.ApplicationId
Set-AzureRmKeyVaultAccessPolicy -VaultName "<your-vault-name>" `
    -ServicePrincipalName $sp.ServicePrincipalName `
    -PermissionsToKeys all -PermissionsToSecrets all `
    -ResourceGroupName "<your-resource-group-name>"

【讨论】:

  • 感谢您的及时回复,即使使用 Set-AzureRmKeyVaultAccessPolicy,它的“值”属性仍然为空(而我之前确实将我的公共证书放入其中)......这是否意味着它真的为空还是出于安全原因只是一个空的财产??
  • 您是否尝试过运行您的应用程序?我能够使用证书进行身份验证并从应用程序成功访问保险库。而且这个值在我的环境中也是空的。是什么让您相信这就是您的问题所在?
  • Rick,你说得对,值中的 null 似乎无关紧要,谢谢。
  • Rick,另一个问题是如果我使用 Graph API 在 Azure AD 中创建应用程序,我可以使用此函数设置所需的访问权限:appObject.RequiredResourceAccess.Add(officeAccess);是否可以使用 Powershell 做同样的事情?当然我可以使用Graph API来创建应用程序并完成整个事情,但是要使用Graph API,我必须先手动创建另一个应用程序,这不是很方便。
  • 我不知道执行此操作的特定 PowerShell 函数。尽管如此,PowerShell 本质上还是封装了 API,只是在一个简化的界面中将它们呈现给您。如果您没有找到执行所需操作的命令,您可以随时使用 PowerShell 直接调用 Graph API。
猜你喜欢
  • 2017-06-18
  • 1970-01-01
  • 2021-10-25
  • 2019-05-28
  • 2022-10-25
  • 2017-02-26
  • 2019-10-15
  • 2019-03-13
  • 1970-01-01
相关资源
最近更新 更多