Azure Keyvault - "The operation "List”未在此 Key Vault 的访问策略中启用。”在以编程方式创建 keyvault 时

Azure Keyvault - "The operation "List" is not enabled in this key vault's access policy." while creating keyvault programmatically

我正在使用 .net core 2.1 和 OpenIdConnect 以及以下 AccessPolicies 创建 azure keyvault

 AccessPolicies = new List<AccessPolicyEntry>()
                {
                    new AccessPolicyEntry
                    {
                        TenantId = Guid.Parse(tenantId),
                        ObjectId = objectId,
                        Permissions = new Permissions
                        {
                          Secrets = new List<string> { "all" },
                          Keys = new string[] { "all" },
                          Certificates = new string[]{"all" }
                        }
                    }
                }

使用它,现在,我可以创建密钥保管库,但是在转到新创建的密钥保管库(在 Azure 门户中)设置时 blade {Key,Secrete,Certificate} 它显示警告 “此密钥保管库的访问策略中未启用操作“列表”。”

注意:- 如上代码所示“All permission are given”。我在azure portal中可以看到。

我试过的:- 我已经尝试参考以下 stack-overflow question-answer

根据上面的 Whosebug 答案“需要传递 Azure AD 应用程序的 服务主体 的 object ID 而不是 object您的 Azure AD 应用程序的 ID。

我尝试使用以下 powershell 脚本找出 object azure AD 应用程序服务主体的 ID

Get-AzADServicePrincipal -ServicePrincipalName "<app client ID>"

它给出以下结果

我尝试在 AccessPolicyEntry 的 objectId 中使用“Id”(在上面的屏幕截图中),但它没有解决问题。

问题:-

  1. 是否需要在AccessPolicyEntry中设置其他权限?
  2. AccessPolicyEntry 中的 objectID 应该是什么(目前,我给出的是 Azure AD 应用程序的 obectId)?
  3. 如果需要 object服务主体的 ID。如何以编程方式获取它?

好吧,我可以在我这边重现你的问题。

首先,pass the object ID of the service principal instead of object ID of your Azure AD application这个操作是完全正确的。在Access policies中将所有权限赋予服务主体后,服务主体将拥有权限。

但是当您检查门户中的 keyvault 时,您使用的是登录 Azure 门户的用户帐户而不是服务主体,这导致了警告。

因此,如果您想修复警告,只需通过门户中的 + Add Access Policy 按钮在 Access policies 中添加您的用户帐户,或者您可以指定您的用户的 object id在您的代码中使用创建密钥库时的权限帐户。

那么关于你的问题:

Is any other permission need to set in AccessPolicyEntry?

没有,权限够了

What should be the objectID in AccessPolicyEntry(currently, I am giving obectId of Azure AD application)?

您不应该使用 AD App 的对象 ID,您的选择是使用 service principal/security group/user account 的对象 ID,这取决于您的要求,详情 here.

If needed objectId of service principal. how can get it programmatically?

您可以像以前一样使用 powershell 命令,或通过服务主体名称使用 Azure CLI az ad sp show

或者,如果您可以使用 Microsoft Graph SDK for C# along with the filter,例如:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var serviceprincipals = await graphClient.Serviceprincipals
    .Request().
    .Filter("some condition").
    .GetAsync();