From 6b976fd8203d39172c618caaea818f0ad906729f Mon Sep 17 00:00:00 2001 From: sayedimac Date: Sun, 9 Mar 2025 12:18:53 +0000 Subject: [PATCH 1/2] simplfy --- Controllers/ResourceGroupsController.cs | 15 ++++----------- Program.cs | 20 ++++---------------- Services/ResourceGroupsClasses.cs | 18 +++++++----------- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/Controllers/ResourceGroupsController.cs b/Controllers/ResourceGroupsController.cs index 369d954..d796fea 100644 --- a/Controllers/ResourceGroupsController.cs +++ b/Controllers/ResourceGroupsController.cs @@ -1,6 +1,4 @@ using Microsoft.AspNetCore.Mvc; -using Microsoft.Identity.Client; -using System.Net.Http.Headers; using System.Text.Json; using AzureIntegration.Services; @@ -12,11 +10,6 @@ public class ResourceGroupsController : Controller { private readonly IHttpClientFactory _httpClientFactory; private readonly IConfiguration _configuration; - private string clientId; - private string clientSecret; - private string tenantId; - private string subscriptionId; - public ResourceGroupsController(IHttpClientFactory httpClientFactory, IConfiguration configuration) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); @@ -37,7 +30,7 @@ public async Task Index() private async Task> GetResourceGroupsAsync() { - var httpClient = _httpClientFactory.CreateClient("AzureManage"); + var httpClient = _httpClientFactory.CreateClient("AzureServices"); var httpResponseMessage = await httpClient.GetAsync( $"resourcegroups?api-version=2021-04-01"); @@ -55,8 +48,8 @@ private async Task> GetResourceGroupsAsync() var location = resourceGroup.GetProperty("location").GetString(); resourceGroups.Add(new ResourceGroup { - Name = name, - Location = location + Name = name ?? string.Empty, + Location = location ?? string.Empty }); } } @@ -67,7 +60,7 @@ private async Task> GetResourceGroupsAsync() private async Task> GetResourcesAsync(string name) { - var httpClient = _httpClientFactory.CreateClient("AzureManage"); + var httpClient = _httpClientFactory.CreateClient("AzureServices"); var httpResponseMessage = await httpClient.GetAsync( $"resourceGroups/{name}/resources?api-version=2021-04-01"); diff --git a/Program.cs b/Program.cs index e4bbe01..503e78d 100644 --- a/Program.cs +++ b/Program.cs @@ -1,16 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.AspNetCore.Mvc; using Microsoft.Identity.Client; -using System.Net.Http; -using System.Threading.Tasks; -using System.Text.Json; -using AzureIntegration.Services; -using System.Collections.Generic; -using Microsoft.Extensions.Configuration; -using Microsoft.AspNetCore.Builder; // Add this line -using Microsoft.AspNetCore.Http; // Add this line -using Microsoft.Net.Http.Headers; // Add this line using System.Net.Http.Headers; // Add this line var builder = WebApplication.CreateBuilder(args); @@ -19,9 +7,9 @@ .AddEnvironmentVariables() .Build(); -var clientId = _configuration["ClientId"]; -var clientSecret = _configuration["ClientSecret"]; -var tenantId = _configuration["TenantId"]; +var clientId = _configuration["ClientId"] ?? throw new ArgumentNullException("ClientId"); +var clientSecret = _configuration["ClientSecret"] ?? throw new ArgumentNullException("ClientSecret"); +var tenantId = _configuration["TenantId"] ?? throw new ArgumentNullException("TenantId"); var subscriptionId = _configuration["SubscriptionId"]; IConfidentialClientApplication authapp = ConfidentialClientApplicationBuilder.Create(clientId) @@ -33,7 +21,7 @@ var authResult = authapp.AcquireTokenForClient(scopes).ExecuteAsync(); -builder.Services.AddHttpClient("AzureManage", httpClient => +builder.Services.AddHttpClient("AzureServices", httpClient => { httpClient.BaseAddress = new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.Result.AccessToken); diff --git a/Services/ResourceGroupsClasses.cs b/Services/ResourceGroupsClasses.cs index 40621fc..92cec28 100644 --- a/Services/ResourceGroupsClasses.cs +++ b/Services/ResourceGroupsClasses.cs @@ -2,21 +2,17 @@ { public class ResourceGroup { - public string Name { get; set; } - public string Location { get; set; } + public string? Name { get; set; } + public string? Location { get; set; } } - public class ResourceGroups - { - public List Value { get; set; } - } public class AzureResource { - public string Id { get; set; } - public string Name { get; set; } - public string Type { get; set; } - public string Location { get; set; } - public Dictionary Tags { get; set; } + public string? Id { get; set; } + public string? Name { get; set; } + public string? Type { get; set; } + public string? Location { get; set; } + public Dictionary? Tags { get; set; } } } From ce5b6ac62518542246e4126d0ff0d49b1202d5e6 Mon Sep 17 00:00:00 2001 From: sayedimac Date: Sun, 9 Mar 2025 12:31:09 +0000 Subject: [PATCH 2/2] optimised more --- Program.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Program.cs b/Program.cs index 503e78d..d403361 100644 --- a/Program.cs +++ b/Program.cs @@ -18,22 +18,18 @@ .Build(); string[] scopes = ["https://management.azure.com/.default"]; -var authResult = authapp.AcquireTokenForClient(scopes).ExecuteAsync(); +var authResult = await authapp.AcquireTokenForClient(scopes).ExecuteAsync(); builder.Services.AddHttpClient("AzureServices", httpClient => { httpClient.BaseAddress = new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/"); - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.Result.AccessToken); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); }); // Add services to the container. builder.Services.AddControllersWithViews(); -// Add configuration -builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddEnvironmentVariables(); - // Register the HTTP client builder.Services.AddHttpClient(); @@ -46,6 +42,7 @@ options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -65,4 +62,4 @@ name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); -app.Run(); +await app.RunAsync();