44var builder = WebApplication . CreateBuilder ( args ) ;
55
66IConfiguration _configuration = new ConfigurationBuilder ( )
7+
78. AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
89 . AddEnvironmentVariables ( )
910 . Build ( ) ;
1011
11- var clientId = _configuration [ "ClientId" ] ?? throw new ArgumentNullException ( "ClientId" ) ;
12- var clientSecret = _configuration [ "ClientSecret" ] ?? throw new ArgumentNullException ( "ClientSecret" ) ;
13- var tenantId = _configuration [ "TenantId" ] ?? throw new ArgumentNullException ( "TenantId" ) ;
12+
13+ var clientId = _configuration [ "ClientId" ] ;
14+ var clientSecret = _configuration [ "ClientSecret" ] ;
15+ var tenantId = _configuration [ "TenantId" ] ;
1416var subscriptionId = _configuration [ "SubscriptionId" ] ;
1517
1618IConfidentialClientApplication authapp = ConfidentialClientApplicationBuilder . Create ( clientId )
17- . WithClientSecret ( clientSecret )
18- . WithAuthority ( new Uri ( $ "https://login.microsoftonline.com/{ tenantId } ") )
19- . Build ( ) ;
19+ . WithClientSecret ( clientSecret )
20+ . WithAuthority ( new Uri ( $ "https://login.microsoftonline.com/{ tenantId } ") )
21+ . Build ( ) ;
2022
2123string [ ] scopes = [ "https://management.azure.com/.default" ] ;
22- var authResult = await authapp . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
24+ var authResult = authapp . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
2325
2426
2527builder . Services . AddHttpClient ( "AzureServices" , httpClient =>
2628{
2729 httpClient . BaseAddress = new Uri ( $ "https://management.azure.com/subscriptions/{ subscriptionId } /") ;
28- httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authResult . AccessToken ) ;
30+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authResult . Result . AccessToken ) ;
2931} ) ;
3032
3133// Add services to the container.
3234builder . Services . AddControllersWithViews ( ) ;
3335
36+ // Add configuration
37+ builder . Configuration . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
38+ . AddEnvironmentVariables ( ) ;
39+
3440// Register the HTTP client
3541builder . Services . AddHttpClient ( ) ;
3642
4349 options . Cookie . HttpOnly = true ;
4450 options . Cookie . IsEssential = true ;
4551} ) ;
46-
4752var app = builder . Build ( ) ;
4853
4954// Configure the HTTP request pipeline.
6065app . UseAuthorization ( ) ;
6166
6267app . MapControllerRoute (
63- name : "default" ,
64- pattern : "{controller=Home}/{action=Index}/{id?}" ) ;
68+ name : "default" ,
69+ pattern : "{controller=Home}/{action=Index}/{id?}" ) ;
6570
66- await app . RunAsync ( ) ;
71+ app . Run ( ) ;
0 commit comments