WHMCS-bridge

Import Key Vault Certificate using C# Print

  • 0

Here is the sample code to import certificate from Key Vault

 

try
{
   string clientId = "";
    string clientSecret = "";
    string tenantId = "";
    string subscriptionId = "";
 
    string resourceGroupName = "sixshotRG";
    string websiteName = "sixshot";
    string certName = "MyCertName";
    string keyVaultId = "/subscriptions/[Key Vault subscription id]/resourcegroups/[key vault resource group name]/providers/microsoft.keyvault/vaults/[key vault name]";
 
    string keyVaultSecretName = "[Key Vault secret or cert name]";
 
    AzureOperationResponse updateCertificateResult;
 
    var credentials = new AzureCredentialsFactory().FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
 
    var azure = Azure
                .Configure()
                .Authenticate(credentials)
                .WithDefaultSubscription();
 
    IWebAppsOperations ops = azure.WebApps.Inner;
 
    RestClient restClient = RestClient
        .Configure()
        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
        //.WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
        .WithCredentials(credentials)
        .Build();
 
    var client = new WebSiteManagementClient(restClient) { SubscriptionId = subscriptionId };
 
    var appDetails = await client.WebApps.GetAsync(resourceGroupName, websiteName);
 
    var certificateInner = new CertificateInner
    {
        KeyVaultId = keyVaultId,
        KeyVaultSecretName = keyVaultSecretName,
        Location = appDetails.Location,
        ServerFarmId = appDetails.ServerFarmId,
        Password =""
    };
 
    updateCertificateResult = await client.Certificates
                .CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    certName,
                    certificateInner);
}
catch(Exception ex)
{
    Console.Write(ex.Message);
}

Was this answer helpful?

« Back

WordPress and WHMCS integration by i-Plugins