Created
August 3, 2021 13:58
-
-
Save karabasosman/35173efd3d3862cc715a4eb7f36ce1a1 to your computer and use it in GitHub Desktop.
Purging Azure CDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Management.Fluent; | |
using Microsoft.Azure.Management.ResourceManager.Fluent; | |
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Az.Demos.Cdn | |
{ | |
class Program | |
{ | |
static readonly string AZURE_CLIENT_ID = ""; | |
static readonly string AZURE_CLIENT_SECRET = ""; | |
static readonly string AZURE_TENANT_ID = ""; | |
static readonly string AZURE_SUBSCRIPTION_ID = ""; | |
static readonly string AZURE_CDN_RESOURCE_GROUP_NAME = ""; | |
static readonly string AZURE_CDN_PROFILE_NAME = ""; | |
static readonly string AZURE_CDN_ENDPOINT_NAME = ""; | |
static async Task Main() | |
{ | |
try | |
{ | |
string token = await GetAccessToken(AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET); | |
var result=await PurgeCdn(token); | |
Console.WriteLine(result); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Exception: {ex.Message}"); | |
} | |
} | |
private static async Task<bool> PurgeCdn(string token) | |
{ | |
Console.WriteLine("Purging CDN"); | |
var httpClient = new HttpClient | |
{ | |
BaseAddress = new Uri("https://management.azure.com/subscriptions/") | |
}; | |
string URI = $"{AZURE_SUBSCRIPTION_ID}/resourceGroups/{AZURE_CDN_RESOURCE_GROUP_NAME}/providers/Microsoft.Cdn/profiles/{AZURE_CDN_PROFILE_NAME}/endpoints/{AZURE_CDN_ENDPOINT_NAME}/purge?api-version=2019-12-31"; | |
httpClient.DefaultRequestHeaders.Remove("Authorization"); | |
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); | |
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); | |
dynamic content = new { ContentPaths = new List<string>() { "/"} }; | |
string purgedContent = JsonConvert.SerializeObject(content); | |
HttpResponseMessage response = await httpClient.PostAsync(URI, new StringContent(purgedContent, Encoding.UTF8, "application/json")); | |
if (response.IsSuccessStatusCode) | |
{ | |
string operationUri = response.Headers.TryGetValues("Azure-AsyncOperation", out IEnumerable<string> values) ? values.FirstOrDefault() : null; | |
for (int i = 0; i < 30; i++) | |
{ | |
await Task.Delay(10000); | |
var result=await CheckOperationResult(operationUri, token); | |
if (result) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
return response.IsSuccessStatusCode; | |
} | |
private static async Task<bool> CheckOperationResult(string operationUri,string token) | |
{ | |
Console.WriteLine("Checking operation"); | |
using (var httpClient=new HttpClient()) | |
{ | |
string URI = operationUri; | |
httpClient.DefaultRequestHeaders.Remove("Authorization"); | |
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); | |
HttpResponseMessage response = await httpClient.GetAsync(URI); | |
var result =await response.Content.ReadAsStringAsync(); | |
var jsonObj = JObject.Parse(result); | |
string status = (string)jsonObj["status"]; | |
return status == "Succeeded"; | |
} | |
} | |
private static async Task<string> GetAccessToken(string tenantId, string clientId, string clientKey) | |
{ | |
Console.WriteLine("Getting access token"); | |
string authContextURL = "https://login.windows.net/" + tenantId; | |
var authenticationContext = new AuthenticationContext(authContextURL); | |
var credential = new ClientCredential(clientId, clientKey); | |
var result = await authenticationContext | |
.AcquireTokenAsync("https://management.azure.com/", credential); | |
if (result == null) | |
{ | |
throw new InvalidOperationException("Failed to obtain the JWT token"); | |
} | |
string token = result.AccessToken; | |
return token; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment