@@ -12,6 +12,7 @@ This template can be used to create a Blazor WASM application hosted in an ASP.N
1212- BFF with Azure B2C using Microsoft.Identity.Web
1313- OAuth2 and OpenID Connect OIDC
1414- No tokens in the browser
15+ - - Azure AD Continuous Access Evaluation CAE support
1516
1617## Other templates
1718
@@ -70,6 +71,112 @@ Add the permissions for Microsoft Graph if required, application scopes are used
7071},
7172```
7273
74+ ### Use Continuous Access Evaluation CAE with a downstream API (access_token)
75+
76+ #### Azure app registration manifest
77+
78+ ``` json
79+ "optionalClaims" : {
80+ "idToken" : [],
81+ "accessToken" : [
82+ {
83+ "name" : " xms_cc" ,
84+ "source" : null ,
85+ "essential" : false ,
86+ "additionalProperties" : []
87+ }
88+ ],
89+ "saml2Token" : []
90+ },
91+ ```
92+
93+ Any API call for the Blazor WASM could be implemented like this:
94+
95+ ```
96+ [HttpGet]
97+ public async Task<IActionResult> Get()
98+ {
99+ try
100+ {
101+ // Do logic which calls an API and throws claims challenge
102+ // WebApiMsalUiRequiredException. The WWW-Authenticate header is set
103+ // using the OpenID Connect standards and Signals spec.
104+ }
105+ catch (WebApiMsalUiRequiredException hex)
106+ {
107+ var claimChallenge = WwwAuthenticateParameters
108+ .GetClaimChallengeFromResponseHeaders(hex.Headers);
109+
110+ return Unauthorized(claimChallenge);
111+ }
112+ }
113+ ```
114+
115+ The downstream API call could be implemented something like this:
116+
117+ ```
118+ public async Task<T> CallApiAsync(string url)
119+ {
120+ var client = _clientFactory.CreateClient();
121+
122+ // ... add bearer token
123+
124+ var response = await client.GetAsync(url);
125+ if (response.IsSuccessStatusCode)
126+ {
127+ var stream = await response.Content.ReadAsStreamAsync();
128+ var payload = await JsonSerializer.DeserializeAsync<T>(stream);
129+
130+ return payload;
131+ }
132+
133+ // You can check the WWW-Authenticate header first, if it is a CAE challenge
134+
135+ throw new WebApiMsalUiRequiredException($"Error: {response.StatusCode}.", response);
136+ }
137+ ```
138+
139+ ### Use Continuous Access Evaluation CAE in a standalone app (id_token)
140+
141+ #### Azure app registration manifest
142+
143+ ``` json
144+ "optionalClaims" : {
145+ "idToken" : [
146+ {
147+ "name" : " xms_cc" ,
148+ "source" : null ,
149+ "essential" : false ,
150+ "additionalProperties" : []
151+ }
152+ ],
153+ "accessToken" : [],
154+ "saml2Token" : []
155+ },
156+ ```
157+ If using a CAE Authcontext in a standalone project, you only need to challenge against the claims in the application.
158+
159+ ```
160+ private readonly CaeClaimsChallengeService _caeClaimsChallengeService;
161+
162+ public AdminApiCallsController(CaeClaimsChallengeService caeClaimsChallengeService)
163+ {
164+ _caeClaimsChallengeService = caeClaimsChallengeService;
165+ }
166+
167+ [HttpGet]
168+ public IActionResult Get()
169+ {
170+ // if CAE claim missing in id token, the required claims challenge is returned
171+ var claimsChallenge = _caeClaimsChallengeService
172+ .CheckForRequiredAuthContextIdToken(AuthContextId.C1, HttpContext);
173+
174+ if (claimsChallenge != null)
175+ {
176+ return Unauthorized(claimsChallenge);
177+ }
178+ ```
179+
73180### uninstall
74181
75182```
@@ -91,7 +198,7 @@ nuget pack content/Blazor.BFF.AzureB2C.Template.nuspec
91198Locally built nupkg:
92199
93200```
94- dotnet new -i Blazor.BFF.AzureB2C.Template.1.1 .0.nupkg
201+ dotnet new -i Blazor.BFF.AzureB2C.Template.1.2 .0.nupkg
95202```
96203
97204Local folder:
@@ -111,7 +218,6 @@ https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-register-ap
111218## Credits, Used NuGet packages + ASP.NET Core 6.0 standard packages
112219
113220- NetEscapades.AspNetCore.SecurityHeaders
114- - IdentityModel.AspNetCore
115221
116222## Links
117223
0 commit comments