Search code examples
azureazure-web-app-servicessl-certificateazure-sdk-.netazure-webapps

How to create or update a certificate using the latest SDK


Does anyone know what the SDK equivalent to this REST API is? Unfortunately, the SDK is so poorly documented that—after two full days of digging—I finally have to ask.

My prior research and things I've tried are detailed here.


Solution

  • In order to Create or Update the Certificate with SDK you can refer this Github sample of Latest Azure.ResourceManager.AppService .Net sample.

    Code from the github sample above:-

    
    // Create Or Update Certificate
            [NUnit.Framework.Test]
            [NUnit.Framework.Ignore("Only verifying that the sample builds")]
            public async Task CreateOrUpdate_CreateOrUpdateCertificate()
            {
                // Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/CreateOrUpdateCertificate.json
                // this example is just showing the usage of "Certificates_CreateOrUpdate" operation, for the dependent resources,
    they will have to be created separately.
    
                // get your azure access token, for more details of how Azure SDK get your access token, please refer to
    https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
                TokenCredential cred = new DefaultAzureCredential();
                // authenticate your client
                ArmClient client = new ArmClient(cred);
    
                // this example assumes you already have this ResourceGroupResource created on azure
                // for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
                string subscriptionId = "xxxxsubidxxxx";
                string resourceGroupName = "rgname";
                ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
    resourceGroupName);
                ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);
    
                // get the collection of this AppCertificateResource
                AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();
    
                // invoke the operation
                string name = "certname";
                AppCertificateData data = new AppCertificateData(new AzureLocation("East US"))
                {
                    Password = "<password>",
                    HostNames = { "ServerCert" },
                };
                ArmOperation<AppCertificateResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, name, data);
                AppCertificateResource result = lro.Value;
    
                // the variable result is a resource, you could call other operations on this instance as well
                // but just for demo, we get its data from this resource instance
                AppCertificateData resourceData = result.Data;
                // for demo we just print out the id
                Console.WriteLine($"Succeeded on id: {resourceData.Id}");
            }
        } 
        }
    
    

    Complete code to manage Certificates of App Service:-

    using Azure.Core; using Azure.Identity; using Azure.ResourceManager;
    using Azure.ResourceManager.AppService; using
    Azure.ResourceManager.Resources; using NUnit;
    
    
    
    namespace Azure.ResourceManager.AppService.Samples {
        public partial class Sample_AppCertificateCollection
        {
            static void Main(string[] args)
            {
            }
            // List Certificates by resource group
            [NUnit.Framework.Test]
            [NUnit.Framework.Ignore("Only verifying that the sample builds")]
            public async Task GetAll_ListCertificatesByResourceGroup()
            {
                // Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/ListCertificatesByResourceGroup.json
                // this example is just showing the usage of "Certificates_ListByResourceGroup" operation, for the dependent
    resources, they will have to be created separately.
    
                // get your azure access token, for more details of how Azure SDK get your access token, please refer to
    https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
                TokenCredential cred = new DefaultAzureCredential();
                // authenticate your client
                ArmClient client = new ArmClient(cred);
    
                // this example assumes you already have this ResourceGroupResource created on azure
                // for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
                string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
                string resourceGroupName = "siliconrg";
                ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
    resourceGroupName);
                ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);
    
                // get the collection of this AppCertificateResource
                AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();
    
                // invoke the operation and iterate over the result
                await foreach (AppCertificateResource item in collection.GetAllAsync())
                {
                    // the variable item is a resource, you could call other operations on this instance as well
                    // but just for demo, we get its data from this resource instance
                    AppCertificateData resourceData = item.Data;
                    // for demo we just print out the id
                    Console.WriteLine($"Succeeded on id: {resourceData.Id}");
                }
    
                Console.WriteLine($"Succeeded");
            }
    
            // Get Certificate
            [NUnit.Framework.Test]
            [NUnit.Framework.Ignore("Only verifying that the sample builds")]
            public async Task Get_GetCertificate()
            {
                // Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/GetCertificate.json
                // this example is just showing the usage of "Certificates_Get" operation, for the dependent resources, they will
    have to be created separately.
    
                // get your azure access token, for more details of how Azure SDK get your access token, please refer to
    https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
                TokenCredential cred = new DefaultAzureCredential();
                // authenticate your client
                ArmClient client = new ArmClient(cred);
    
                // this example assumes you already have this ResourceGroupResource created on azure
                // for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
                string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
                string resourceGroupName = "siliconrg";
                ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
    resourceGroupName);
                ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);
    
                // get the collection of this AppCertificateResource
                AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();
    
                // invoke the operation
                string name = "certsilicon";
                AppCertificateResource result = await collection.GetAsync(name);
    
                // the variable result is a resource, you could call other operations on this instance as well
                // but just for demo, we get its data from this resource instance
                AppCertificateData resourceData = result.Data;
                // for demo we just print out the id
                Console.WriteLine($"Succeeded on id: {resourceData.Id}");
            }
    
            // Get Certificate
            [NUnit.Framework.Test]
            [NUnit.Framework.Ignore("Only verifying that the sample builds")]
            public async Task Exists_GetCertificate()
            {
                // Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/GetCertificate.json
                // this example is just showing the usage of "Certificates_Get" operation, for the dependent resources, they will
    have to be created separately.
    
                // get your azure access token, for more details of how Azure SDK get your access token, please refer to
    https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
                TokenCredential cred = new DefaultAzureCredential();
                // authenticate your client
                ArmClient client = new ArmClient(cred);
    
                // this example assumes you already have this ResourceGroupResource created on azure
                // for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
                string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
                string resourceGroupName = "siliconrg";
                ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
    resourceGroupName);
                ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);
    
                // get the collection of this AppCertificateResource
                AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();
    
                // invoke the operation
                string name = "certsilicon";
                bool result = await collection.ExistsAsync(name);
    
                Console.WriteLine($"Succeeded: {result}");
            }
    
            // Create Or Update Certificate
            [NUnit.Framework.Test]
            [NUnit.Framework.Ignore("Only verifying that the sample builds")]
            public async Task CreateOrUpdate_CreateOrUpdateCertificate()
            {
                // Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/CreateOrUpdateCertificate.json
                // this example is just showing the usage of "Certificates_CreateOrUpdate" operation, for the dependent resources,
    they will have to be created separately.
    
                // get your azure access token, for more details of how Azure SDK get your access token, please refer to
    https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
                TokenCredential cred = new DefaultAzureCredential();
                // authenticate your client
                ArmClient client = new ArmClient(cred);
    
                // this example assumes you already have this ResourceGroupResource created on azure
                // for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
                string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
                string resourceGroupName = "siliconrg";
                ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
    resourceGroupName);
                ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);
    
                // get the collection of this AppCertificateResource
                AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();
    
                // invoke the operation
                string name = "certsilicon";
                AppCertificateData data = new AppCertificateData(new AzureLocation("East US"))
                {
                    Password = "password",
                    HostNames = { "ServerCert" },
                };
                ArmOperation<AppCertificateResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, name, data);
                AppCertificateResource result = lro.Value;
    
                // the variable result is a resource, you could call other operations on this instance as well
                // but just for demo, we get its data from this resource instance
                AppCertificateData resourceData = result.Data;
                // for demo we just print out the id
                Console.WriteLine($"Succeeded on id: {resourceData.Id}");
            }
        } } 
    

    Output:-

    enter image description here