Create Azure Key Vault via c# with RBAC.

ASR 671 Reputation points
2024-04-10T06:52:54.9233333+00:00

Hi,

I am using below code to create Azure Key vault and below is my code.

Where azure is an object of IAzure. This code is working fine. But when I am visiting this vault in Azure I can see "Permission" Model within "Acces Configuration" is set to "Vault access policy".

I want this vault to use "Azure role-based access control (recommended)" permission model.

azure.Vaults.Define(mymodel.vaultName) 
.WithRegion(mymodel.region)

 .WithExistingResourceGroup(mymodel.resourceGroup)

 .DefineAccessPolicy()

     .ForObjectId(azureClientApplication == null ? mymodel.clientObjectId : azureClientApplication.clientAppObjectId)

     .AllowCertificatePermissions(CertificatePermissions.Import, CertificatePermissions.Get, CertificatePermissions.List)

     .AllowSecretAllPermissions()

     .Attach()

 .Create();
			
Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,118 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,385 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,175 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marilee Turscak-MSFT 33,876 Reputation points Microsoft Employee
    2024-04-11T23:24:25.1466667+00:00

    Hi @ASR ,

    The "EnableRbacAuthorization" parameter allows you to set the permission model to RBAC programmatically. You need to make sure this parameter is set to "true".

    Powershell example:

    Update-AzKeyVault -Name MyKeyVault -ResourceGroupName KeyVaults -EnableRbacAuthorization $true

    C# example of setting the property:

    [Newtonsoft.Json.JsonProperty(PropertyName="enableRbacAuthorization")] public bool? EnableRbacAuthorization { get; set; }

    Let me know if either of these examples help and if you have further questions.

    If the information helped you, please Accept the answer. This will help us as well as others in the community who may be researching similar questions.


0 additional answers

Sort by: Most helpful