Skip to content

Commit 0942f68

Browse files
committed
shorter
1 parent 826cf28 commit 0942f68

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

Program.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,36 @@
44
var builder = WebApplication.CreateBuilder(args);
55

66
IConfiguration _configuration = new ConfigurationBuilder()
7-
.AddEnvironmentVariables()
8-
.Build();
7+
.AddEnvironmentVariables()
8+
.Build();
99

10-
var clientId = _configuration["ClientId"] ?? throw new ArgumentNullException("ClientId");
11-
var clientSecret = _configuration["ClientSecret"] ?? throw new ArgumentNullException("ClientSecret");
12-
var tenantId = _configuration["TenantId"] ?? throw new ArgumentNullException("TenantId");
10+
var clientId = _configuration["ClientId"];
11+
var clientSecret = _configuration["ClientSecret"];
12+
var tenantId = _configuration["TenantId"];
1313
var subscriptionId = _configuration["SubscriptionId"];
1414

1515
IConfidentialClientApplication authapp = ConfidentialClientApplicationBuilder.Create(clientId)
16-
.WithClientSecret(clientSecret)
17-
.WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
18-
.Build();
16+
.WithClientSecret(clientSecret)
17+
.WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
18+
.Build();
1919

2020
string[] scopes = ["https://management.azure.com/.default"];
21-
var authResult = await authapp.AcquireTokenForClient(scopes).ExecuteAsync();
21+
var authResult = authapp.AcquireTokenForClient(scopes).ExecuteAsync();
2222

2323

2424
builder.Services.AddHttpClient("AzureServices", httpClient =>
2525
{
2626
httpClient.BaseAddress = new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/");
27-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
27+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.Result.AccessToken);
2828
});
2929

3030
// Add services to the container.
3131
builder.Services.AddControllersWithViews();
3232

33+
// Add configuration
34+
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
35+
.AddEnvironmentVariables();
36+
3337
// Register the HTTP client
3438
builder.Services.AddHttpClient();
3539

@@ -42,7 +46,6 @@
4246
options.Cookie.HttpOnly = true;
4347
options.Cookie.IsEssential = true;
4448
});
45-
4649
var app = builder.Build();
4750

4851
// Configure the HTTP request pipeline.
@@ -59,7 +62,7 @@
5962
app.UseAuthorization();
6063

6164
app.MapControllerRoute(
62-
name: "default",
63-
pattern: "{controller=Home}/{action=Index}/{id?}");
65+
name: "default",
66+
pattern: "{controller=Home}/{action=Index}/{id?}");
6467

65-
await app.RunAsync();
68+
app.Run();

0 commit comments

Comments
 (0)