44var builder = WebApplication . CreateBuilder ( args ) ;
55
66IConfiguration _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" ] ;
1313var subscriptionId = _configuration [ "SubscriptionId" ] ;
1414
1515IConfidentialClientApplication 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
2020string [ ] scopes = [ "https://management.azure.com/.default" ] ;
21- var authResult = await authapp . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
21+ var authResult = authapp . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
2222
2323
2424builder . 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.
3131builder . Services . AddControllersWithViews ( ) ;
3232
33+ // Add configuration
34+ builder . Configuration . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
35+ . AddEnvironmentVariables ( ) ;
36+
3337// Register the HTTP client
3438builder . Services . AddHttpClient ( ) ;
3539
4246 options . Cookie . HttpOnly = true ;
4347 options . Cookie . IsEssential = true ;
4448} ) ;
45-
4649var app = builder . Build ( ) ;
4750
4851// Configure the HTTP request pipeline.
5962app . UseAuthorization ( ) ;
6063
6164app . 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