1
+ #if ( GenerateApi )
2
+ using System . Net . Http ;
3
+ #endif
4
+ using Microsoft . AspNetCore . Authentication ;
5
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
6
+ #if ( GenerateGraph )
7
+ using Graph = Microsoft . Graph ;
8
+ #endif
9
+ using Microsoft . Identity . Web ;
10
+ using Microsoft . Identity . Web . Resource ;
11
+
12
+ var builder = WebApplication . CreateBuilder ( args ) ;
13
+
14
+ // Add services to the container.
15
+ #if ( OrganizationalAuth )
16
+ builder . Services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
17
+ #if ( GenerateApiOrGraph )
18
+ . AddMicrosoftIdentityWebApi ( builder . Configuration . GetSection ( "AzureAd" ) )
19
+ . EnableTokenAcquisitionToCallDownstreamApi ( )
20
+ #if ( GenerateApi )
21
+ . AddDownstreamWebApi ( "DownstreamApi" , builder . Configuration . GetSection ( "DownstreamApi" ) )
22
+ #endif
23
+ #if ( GenerateGraph )
24
+ . AddMicrosoftGraph ( builder . Configuration . GetSection ( "DownstreamApi" ) )
25
+ #endif
26
+ . AddInMemoryTokenCaches ( ) ;
27
+ #else
28
+ . AddMicrosoftIdentityWebApi ( builder . Configuration . GetSection ( "AzureAd" ) ) ;
29
+ #endif
30
+ #elif ( IndividualB2CAuth )
31
+ builder . Services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
32
+ #if ( GenerateApi )
33
+ . AddMicrosoftIdentityWebApi ( builder . Configuration . GetSection ( "AzureAdB2C" ) )
34
+ . EnableTokenAcquisitionToCallDownstreamApi ( )
35
+ . AddDownstreamWebApi ( "DownstreamApi" , builder . Configuration . GetSection ( "DownstreamApi" ) )
36
+ . AddInMemoryTokenCaches ( ) ;
37
+ #else
38
+ . AddMicrosoftIdentityWebApi ( builder . Configuration . GetSection ( "AzureAdB2C" ) ) ;
39
+ #endif
40
+ #endif
41
+ builder . Services . AddAuthorization ( ) ;
42
+
43
+ #if ( EnableOpenAPI )
44
+ builder . Services . AddEndpointsApiExplorer ( ) ;
45
+ builder . Services . AddSwaggerGen ( ) ;
46
+ #endif
47
+
48
+ var app = builder . Build ( ) ;
49
+
50
+ // Configure the HTTP request pipeline.
51
+ #if ( EnableOpenAPI )
52
+ if ( app . Environment . IsDevelopment ( ) )
53
+ {
54
+ app . UseSwagger ( ) ;
55
+ app . UseSwaggerUI ( ) ;
56
+ }
57
+ #endif
58
+ #if ( RequiresHttps )
59
+
60
+ app . UseHttpsRedirection ( ) ;
61
+ #endif
62
+
63
+ app . UseAuthentication ( ) ;
64
+ app . UseAuthorization ( ) ;
65
+
66
+ var scopeRequiredByApi = app . Configuration [ "AzureAd:Scopes" ] ;
67
+ var summaries = new [ ]
68
+ {
69
+ "Freezing" , "Bracing" , "Chilly" , "Cool" , "Mild" , "Warm" , "Balmy" , "Hot" , "Sweltering" , "Scorching"
70
+ } ;
71
+
72
+ #if ( GenerateApi )
73
+ app . MapGet ( "/weatherforecast" , ( HttpContext httpContext , IDownstreamWebApi downstreamWebApi ) =>
74
+ {
75
+ httpContext . VerifyUserHasAnyAcceptedScope ( scopeRequiredByApi ) ;
76
+
77
+ using var response = await downstreamWebApi . CallWebApiForUserAsync ( "DownstreamApi" ) . ConfigureAwait ( false ) ;
78
+ if ( response . StatusCode == System . Net . HttpStatusCode . OK )
79
+ {
80
+ var apiResult = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
81
+ // Do something
82
+ }
83
+ else
84
+ {
85
+ var error = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
86
+ throw new HttpRequestException ( $ "Invalid status code in the HttpResponseMessage: { response . StatusCode } : { error } ") ;
87
+ }
88
+
89
+ var forecast = Enumerable . Range ( 1 , 5 ) . Select ( index =>
90
+ new WeatherForecast
91
+ (
92
+ DateTime . Now . AddDays ( index ) ,
93
+ Random . Shared . Next ( - 20 , 55 ) ,
94
+ summaries [ Random . Shared . Next ( summaries . Length ) ]
95
+ ) )
96
+ . ToArray ( ) ;
97
+
98
+ return forecast ;
99
+ } )
100
+ #elseif ( GenerateGraph )
101
+ app. MapGet ( "/weahterforecast" , ( HttpContext httpContext , GraphServiceClient graphServiceClient ) =>
102
+ {
103
+ httpContext . VerifyUserHasAnyAcceptedScope ( scopeRequiredByApi ) ;
104
+
105
+ var user = await _graphServiceClient . Me . Request ( ) . GetAsync ( ) ;
106
+
107
+ var forecast = Enumerable . Range ( 1 , 5 ) . Select ( index =>
108
+ new WeatherForecast
109
+ (
110
+ DateTime . Now . AddDays ( index ) ,
111
+ Random . Shared . Next ( - 20 , 55 ) ,
112
+ summaries [ Random . Shared . Next ( summaries . Length ) ]
113
+ ) )
114
+ . ToArray ( ) ;
115
+
116
+ return forecast ;
117
+ } )
118
+ #else
119
+ app. MapGet ( "/weatherforecast" , ( HttpContext httpContext ) =>
120
+ {
121
+ httpContext . VerifyUserHasAnyAcceptedScope ( scopeRequiredByApi ) ;
122
+
123
+ var forecast = Enumerable . Range ( 1 , 5 ) . Select ( index =>
124
+ new WeatherForecast
125
+ (
126
+ DateTime . Now . AddDays ( index ) ,
127
+ Random . Shared . Next ( - 20 , 55 ) ,
128
+ summaries [ Random . Shared . Next ( summaries . Length ) ]
129
+ ) )
130
+ . ToArray ( ) ;
131
+ return forecast ;
132
+ #endif
133
+ #if ( EnableOpenAPI )
134
+ } )
135
+ . WithName ( "GetWeatherForecast" )
136
+ . RequireAuthorization ( ) ;
137
+ #else
138
+ } )
139
+ . RequireAuthorization ( ) ;
140
+ #endif
141
+
142
+ app. Run ( ) ;
143
+
144
+ record WeatherForecast ( DateTime Date , int TemperatureC , string ? Summary )
145
+ {
146
+ public int TemperatureF => 32 + ( int ) ( TemperatureC / 0.5556 ) ;
147
+ }
0 commit comments