Created
April 6, 2022 19:13
-
-
Save sonuame/3444b02c70fc00388ab659a1f162243c to your computer and use it in GitHub Desktop.
Azure Active Directory
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
public static void GetAuthorizationToken() | |
{ | |
ClientCredential cc = new ClientCredential(AzureDetails.ClientID, AzureDetails.ClientSecret); | |
var context = new AuthenticationContext("https://login.microsoftonline.com/" + AzureDetails.TenantID); | |
var result = context.AcquireTokenAsync("https://management.azure.com/", cc); | |
if (result == null) | |
{ | |
throw new InvalidOperationException("Failed to obtain the Access token"); | |
} | |
AzureDetails.AccessToken = result.Result.AccessToken; | |
} |
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
public static async Task getAllResourceGroupDetails() | |
{ | |
HttpClient client = new HttpClient(); | |
client.BaseAddress = new Uri("https://management.azure.com/subscriptions/" + AzureDetails.SubscriptionID + "/resourcegroups?api-version=2019-10-01"); | |
client.DefaultRequestHeaders.Accept.Clear(); | |
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AzureDetails.AccessToken); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress); | |
var response = await MakeRequestAsync(request, client); | |
AzureDetails.Response = response; | |
} | |
public static async Task createResourceGroup() | |
{ | |
HttpClient client = new HttpClient(); | |
client.BaseAddress = new Uri("https://management.azure.com/subscriptions/" + AzureDetails.SubscriptionID + "/resourcegroups/" + AzureDetails.ResourceGroupName + "?api-version=2019-10-01"); | |
client.DefaultRequestHeaders.Accept.Clear(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AzureDetails.AccessToken); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, client.BaseAddress); | |
var body = $"{{\"location\": \"{AzureDetails.Location}\"}}"; | |
var content = new StringContent(body, Encoding.UTF8, "application/json"); | |
request.Content = content; | |
var response = await MakeRequestAsync(request, client); | |
AzureDetails.Response = response; | |
} | |
public static async Task<string> MakeRequestAsync(HttpRequestMessage getRequest, HttpClient client) | |
{ | |
var response = await client.SendAsync(getRequest).ConfigureAwait(false); | |
var responseString = string.Empty; | |
try | |
{ | |
response.EnsureSuccessStatusCode(); | |
responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | |
} | |
catch (HttpRequestException) | |
{ | |
} | |
return responseString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment