> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using NET Azure CDN Library

> Guide to using the Azure CDN .NET SDK to authenticate, create, list, manage, and purge CDN profiles and endpoints for automation, deployment pipelines, and monitoring.

Using the .NET Azure Content Delivery Network (CDN) library to automate creation, management, and monitoring of CDN profiles and endpoints.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4asMmWJpkl1BXMJG/images/AZ-204-Developing-Solutions-for-Microsoft-Azure/Developing-for-storage-on-CDNs/Using-NET-Azure-CDN-Library/dotnet-azure-cdn-library-slide.jpg?fit=max&auto=format&n=4asMmWJpkl1BXMJG&q=85&s=5b5f82588a8145d1d80474ee1af8cfc7" alt="A presentation slide titled &#x22;Using .NET Azure Content Delivery Network Library&#x22; with a stylized blue .NET logo on a teal curved background. The slide also shows a small &#x22;© Copyright KodeKloud&#x22; note in the corner." width="1920" height="1080" data-path="images/AZ-204-Developing-Solutions-for-Microsoft-Azure/Developing-for-storage-on-CDNs/Using-NET-Azure-CDN-Library/dotnet-azure-cdn-library-slide.jpg" />
</Frame>

Overview

The Azure CDN .NET SDK lets .NET developers manage Azure CDN resources programmatically, enabling integration with applications, deployment pipelines, and automation scripts. Typical tasks you can perform with the SDK include:

* Creating and configuring CDN profiles and endpoints.
* Listing and inspecting CDN profiles and their endpoints.
* Purging endpoint caches to force fresh content from the origin.
* Retrieving configuration and operational details for monitoring and automation.

What you'll learn

* How to authenticate the .NET SDK to Azure using DefaultAzureCredential.
* How to instantiate a CdnManagementClient.
* How to list CDN profiles in a resource group and enumerate their endpoints.
* Notes about permissions and purging cached content.

Quick reference: common SDK actions

| Action | SDK method / service |
| - | - |
| Create client | `CdnManagementClient` (requires `TokenCredentials`) |
| List profiles | `Profiles.ListByResourceGroup(resourceGroupName)` |
| List endpoints for profile | `Endpoints.ListByProfile(resourceGroupName, profileName)` |
| Purge endpoint | `Endpoints.PurgeContent(resourceGroupName, profileName, endpointName, contentPurgeParameters)` |

Prerequisites

* An Azure subscription and a resource group containing CDN profiles.
* Appropriate Azure RBAC role (for listing, Reader on the resource group is sufficient; for create/purge you need Contributor or specific CDN roles).
* .NET 6+ (or supported .NET version) development environment.
* NuGet packages:
  * `Azure.Identity` (for DefaultAzureCredential)
  * `Azure.Core`
  * `Microsoft.Azure.Management.Cdn`
  * `Microsoft.Rest.ClientRuntime` (for `TokenCredentials`)

Install packages (example)

```bash theme={null}
dotnet add package Azure.Identity
dotnet add package Microsoft.Azure.Management.Cdn
dotnet add package Microsoft.Rest.ClientRuntime
```

Authentication note

<Callout icon="lightbulb" color="#1CB2FE">
  `DefaultAzureCredential` attempts multiple authentication methods (environment variables, managed identity, Visual Studio/VS Code sign-in, Azure CLI, etc.). When running locally, sign in with the Azure CLI (`az login`) or Visual Studio/VS Code. The caller must have the required Azure RBAC permissions to list or manage CDN resources.
</Callout>

Example: List CDN profiles and endpoints using the Azure .NET SDK

This concise example demonstrates:

* Acquiring an Azure AD token using `DefaultAzureCredential`.
* Converting that token into a `TokenCredentials` instance for the management client.
* Listing all CDN profiles in a resource group and enumerating their endpoints.

```csharp theme={null}
// Example: List CDN profiles and endpoints using Azure .NET SDK
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.Management.Cdn;
using Microsoft.Azure.Management.Cdn.Models;
using Microsoft.Rest;

class Program
{
    static async Task Main(string[] args)
    {
        // Replace with your subscription ID and resource group name
        string subscriptionId = "548f7d26-b5b1-468e-ad45-6ee12accf7e7";
        string resourceGroupName = "rg-az204-cdn";

        // Authenticate using DefaultAzureCredential
        var credential = new DefaultAzureCredential();
        var tokenRequestContext = new TokenRequestContext(new[] { "https://management.azure.com/.default" });
        AccessToken token = await credential.GetTokenAsync(tokenRequestContext);

        // Create service client credentials from the access token
        var serviceClientCredentials = new TokenCredentials(token.Token);

        // Instantiate the CDN management client
        using var cdnClient = new CdnManagementClient(serviceClientCredentials)
        {
            SubscriptionId = subscriptionId
        };

        // List profiles and endpoints
        ListProfilesAndEndpoints(cdnClient, resourceGroupName);
    }

    static void ListProfilesAndEndpoints(CdnManagementClient cdnClient, string resourceGroupName)
    {
        // List all CDN profiles in the specified resource group
        var profileList = cdnClient.Profiles.ListByResourceGroup(resourceGroupName);
        foreach (Profile p in profileList)
        {
            Console.WriteLine($"CDN profile: {p.Name}");

            // List all endpoints for this profile.
            // Note: ListByProfile takes (resourceGroupName, profileName)
            var endpointList = cdnClient.Endpoints.ListByProfile(resourceGroupName, p.Name);
            Console.WriteLine("Endpoints:");
            foreach (Endpoint e in endpointList)
            {
                Console.WriteLine($"- {e.Name} ({e.HostName})");
            }
            Console.WriteLine();
        }
    }
}
```

Explanation and important notes

* Authentication: The sample uses `DefaultAzureCredential` to obtain an Azure AD access token scoped for Azure Resource Manager (`https://management.azure.com/.default`). That token is wrapped in a `TokenCredentials` instance required by `CdnManagementClient`.
* SubscriptionId: Set to the subscription containing your CDN profiles.
* Resource group: Provide the name of the resource group that contains your CDN profiles when calling `ListByResourceGroup`.
* Correct parameter order: To list endpoints for a profile, call `Endpoints.ListByProfile(resourceGroupName, profileName)` — the first parameter is the resource group name, the second is the profile name.
* Purging content: The SDK supports endpoint purge operations (e.g., `Endpoints.PurgeContent`), which you use to clear cached content so updated content is fetched from the origin.
* Permissions: For read operations, `Reader` on the resource group is sufficient; for create/update/purge operations, use `Contributor` or granular CDN permissions.

Further resources

* [Azure CDN overview](https://learn.microsoft.com/azure/cdn/)
* [Azure .NET SDK authentication (DefaultAzureCredential)](https://learn.microsoft.com/dotnet/api/azure.identity.defaultazurecredential)
* [Microsoft.Azure.Management.Cdn NuGet package](https://www.nuget.org/packages/Microsoft.Azure.Management.Cdn)

If you prefer portal or CLI workflows, the [Azure portal](https://portal.azure.com/), [Azure CLI](https://learn.microsoft.com/cli/azure/), and [Azure PowerShell](https://learn.microsoft.com/azure/powershell/) are fully supported alternatives. The .NET SDK is ideal when you need to embed CDN management into applications or automation pipelines.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/az-204-developing-solutions-for-microsoft-azure/module/892d3923-3191-43b5-a566-3db2ad980268/lesson/a380770f-030a-43d4-9c33-e63ef501d5a6" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.