.netazureblazorsdk

Delete all custom Domain certificates AppService


I created a form where I can add but also delete a custom domain and certificate. I delete the domain in the following way:

var hostBinding = hostnameBindings.Get(_customDomain);
if (hostBinding != null)
{
    SiteHostNameBindingResource siteHostNameBinding = hostBinding.Value;
    await siteHostNameBinding.DeleteAsync(WaitUntil.Completed);
}

But I don't know how to delete the certificate:

AppCertificateCollection certcollection = _resourceGroupResource.GetAppCertificates();

If I know the name of the certificate, I can do it like this:

var certificate = certcollection.Exists("name");
certificate.Delete();

Solution

  • try out the c# code below below. it will get all resources in the RG and see if the resource is a cert. If it is a cert, you can invoke the DeleteAsync() to delete the cert. MSFT documentation here for AppCertificateResource. https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.appservice.appcertificateresource?view=azure-dotnet

    using System;
    using System.Threading.Tasks;
    using Azure.Identity;
    using Azure.ResourceManager;
    using Azure.ResourceManager.Resources;
    using Azure.Core;
    using Azure.Security.KeyVault.Certificates;
    using Azure.ResourceManager.AppService;
    
    public class AzureResourceGroupExample
    {
        public static async Task Main(string[] args)
        {
            string subscriptionId = "xxxx";
            string resourceGroupName = "rg-xxx";
    
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            ResourceIdentifier subscriptionResourceId = new ResourceIdentifier($"/subscriptions/{subscriptionId}");
            SubscriptionResource subscription = armClient.GetSubscriptionResource(subscriptionResourceId);
            ResourceGroupResource _resourceGroupResource = await subscription.GetResourceGroupAsync(resourceGroupName);
            Console.WriteLine($"Resource group retrieved: {_resourceGroupResource.Data.Name}");
    
            await foreach (var resource in _resourceGroupResource.GetGenericResourcesAsync())
            {
                if (resource.Data.ResourceType == "Microsoft.Web/certificates")
                {
                    var certificateResource = await _resourceGroupResource.GetAppCertificateAsync(resource.Data.Name);
                    Console.WriteLine($"- Certificate Resource: {certificateResource.Value.Id}");
                    // certificateResource.Value.DeleteAsync();
                }
                else
                {
                    Console.WriteLine($"- Resource: {resource.Data.Name}, Type: {resource.Data.ResourceType}");
                }
            }
    
        }
    }