[Azure CDN] How to Purge CDN content from C# code


Recently I had a chance to work on this ask where one of the developer wanted a sample code to purge the CDN content by hitting this REST API instead portal. Initially I followed this link but do not wanted to give my username and password as explained there for AAD authentication. I wanted to get my app registered with AAD as WebApp and then work with ClientID and Client Secret way to the get the bearer token for making the REST API call. It worked rightly after so many attempts, so would like to leave the steps here for easy reference.

Step1: Register your Web Application in AAD (portal.Azure.com) and get the Client ID and Secret generated.

8

Step2) Select the required permissions as below.

7

6

Step3) Add this application as a contributor to the CDN Endpoint which we wanted to purge.

CDN Profile > CDN Endpoint > Access Control (IAM) > Add our Web Application which is going to hit this endpoint to purge

10

9

Step4) Now run our sample code to get invoke purged call

image       

Step5) On success.

5

Sample C# code used for purge ( from WebApplication )

       public ActionResult Contact()
        {
            GetAccessTokenAndMakePurgeCall();

            ViewBag.Result = “Done – Purged all.. “;

            return View();
        }

        private static void GetAccessTokenAndMakePurgeCall()
        {
            string clientId = “e32a947c-136e-xxxxxx-15eed998b592”;
            string clientSecret = “Ga9y3/9wNklz3Ft/xxxxxxxxgqpNM5KZxxxXM=”;
            string uri = @”
https://management.azure.com/subscriptions/xxxxxx-xxxxx/resourcegroups/emptybincdnrg/providers/Microsoft.Cdn/profiles/bluehousecdn/endpoints/bhep/purge?api-version=2015-06-01″;

            var authenticationContext = new AuthenticationContext(“https://login.microsoftonline.com/microsoft.onmicrosoft.com”);
            ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
            Task<AuthenticationResult> resultstr = authenticationContext.AcquireTokenAsync(“
https://management.core.windows.net/”, clientCredential);

            WebClient client = new WebClient();
            //authentication using the Azure AD application
            var token = resultstr.Result.AccessToken;
            client.Headers.Add(HttpRequestHeader.Authorization, “Bearer ” + token);
            client.Headers.Add(“api-version:2015-06-01”);
            client.Headers.Add(“Content-Type”, “application/json”);

            var bodyText = string.Empty;

            //For individual files
            //dynamic content = new { ContentPaths = new List<string>() { “/1.jpg”, “/2.jpg” } };

            //bodyText = JsonConvert.SerializeObject(content);

            //For purge all (*.*)
            //bodyText = “{\”ContentPaths\”:[\”/*\”]}”;

            try
            {
                var result = client.UploadString(uri, bodyText);
            }
            catch (Exception ew)
            {
                //handle the exception here
            }
        }

 

On running fiddler, we can see the header details are formed correctly and getting the bearer token to proceed.

image

On success, you could see “202” in the response which means our purge request has been accepted.

image

 

<update:4/18>

How to purge the CDN content using Postman ( assuming you have the valid bearer token )

1

2

On success, you would see “202” – accepted.

</update>

Hope this helps.

 

<Disclaimer> 

              a) This sample code provided here is for the purpose of illustration only and is not intended to be used in a production environment as it is.

              b) ContentPaths should be carefully checked. The path “/*” should be used only when you want to purge all the contents otherwise specify the resource path.

</Disclaimer> 

Comments (0)

Skip to main content