Dismiss Notice

Register now to be one of the first members of this SharePoint Community! Click here it just takes seconds!

Dismiss Notice
Welcome Guest from Country Flag

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

Discussion in 'Official Microsoft News' started by Maheshk [MSFT], Mar 31, 2017.

Thread Status:
Not open for further replies.
  1. Maheshk [MSFT]

    Maheshk [MSFT] Guest

    Blog Posts:
    0
    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.

    [​IMG]

    Step2) Select the required permissions as below.

    [​IMG]

    [​IMG]

    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

    [​IMG]

    [​IMG]

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

    [​IMG]

    Step5) On success.

    [​IMG]

    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.


    [​IMG]

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

    [​IMG]


    Hope this helps.

    Continue reading...
     
Thread Status:
Not open for further replies.

Share This Page

LiveZilla Live Chat Software