Skip to content

Commit 39a2b3c

Browse files
authored
Merge pull request #77 from Azure-Samples/pmaytak/miw-0.2.0
Update to Identity Web 0.2.0-preview
2 parents 24277ec + acd78ae commit 39a2b3c

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

2-Call-OwnApi/README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ The relevant code for this sample is in the `Program.cs` file, in the `RunAsync(
265265

266266
### TodoList Web API Code
267267

268-
The relevant code for the Web API is on the `Startup.cs` class. We are using the method `AddProtectWebApiWithMicrosoftIdentityPlatformV2` to configure the Web API to authenticate using bearer tokens, validate them and protect the API from non authorized calls. These are the steps:
268+
The relevant code for the Web API is in the `Startup.cs` class. We are using the method `AddMicrosoftWebApiAuthentication` to configure the Web API to authenticate using bearer tokens, validate them and protect the API from non authorized calls. These are the steps:
269269

270270
1. Configuring the API to authenticate using bearer tokens
271271

@@ -274,41 +274,56 @@ The relevant code for the Web API is on the `Startup.cs` class. We are using the
274274
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
275275
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
276276

277-
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
278-
.AddAzureADBearer(options => configuration.Bind("AzureAd", options));
277+
services.AddMicrosoftWebApiAuthentication(Configuration);
279278

280-
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
279+
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
281280
{
282-
configuration.Bind("AzureAd", options);
283-
options.Authority += "/v2.0";
284281
options.TokenValidationParameters.RoleClaimType = "roles";
285282
});
286283
```
287284

288285
2. Validating the tokens
289286

290-
The `AadIssuerValidator.GetIssuerValidator` method can be found on `Microsoft.Identity.Web` project.
287+
As a result of the above `AddMicrosoftWebApiAuthentication` method, some audience and issuer validation is set up. More information can be found in [Microsoft Identity Web](https://github.com/AzureAD/microsoft-identity-web) project.
291288
292289
```CSharp
293-
options.TokenValidationParameters.ValidAudiences = new string[] { options.Audience, $"api://{options.Audience}" };
294-
options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.GetIssuerValidator(options.Authority).Validate;
290+
if (options.TokenValidationParameters.AudienceValidator == null
291+
&& options.TokenValidationParameters.ValidAudience == null
292+
&& options.TokenValidationParameters.ValidAudiences == null)
293+
{
294+
RegisterValidAudience registerAudience = new RegisterValidAudience();
295+
registerAudience.RegisterAudienceValidation(
296+
options.TokenValidationParameters,
297+
microsoftIdentityOptions.Value);
298+
}
299+
300+
// If the developer registered an IssuerValidator, do not overwrite it
301+
if (options.TokenValidationParameters.IssuerValidator == null)
302+
{
303+
// Instead of using the default validation (validating against a single tenant, as we do in line of business apps),
304+
// we inject our own multi-tenant validation logic (which even accepts both v1.0 and v2.0 tokens)
305+
options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.GetIssuerValidator(options.Authority).Validate;
306+
}
295307
```
296308

297309
3. Protecting the Web API
298310

299311
Only apps that have added the **application role** created on **Azure Portal** for the `TodoList-webapi-daemon-v2`, will contain the claim `roles` on their tokens
300312

301313
```CSharp
314+
var tokenValidatedHandler = options.Events.OnTokenValidated;
302315
options.Events.OnTokenValidated = async context =>
303316
{
304317
// This check is required to ensure that the Web API only accepts tokens from tenants where it has been consented and provisioned.
305318
if (!context.Principal.Claims.Any(x => x.Type == ClaimConstants.Scope)
306-
&& !context.Principal.Claims.Any(y => y.Type == ClaimConstants.Roles))
319+
&& !context.Principal.Claims.Any(y => y.Type == ClaimConstants.Scp)
320+
&& !context.Principal.Claims.Any(y => y.Type == ClaimConstants.Roles)
321+
&& !context.Principal.Claims.Any(y => y.Type == ClaimConstants.Role))
307322
{
308323
throw new UnauthorizedAccessException("Neither scope or roles claim were found in the bearer token.");
309324
}
310325

311-
await Task.FromResult(0);
326+
await tokenValidatedHandler(context).ConfigureAwait(false);
312327
};
313328
```
314329

2-Call-OwnApi/TodoList-WebApi/TodoList-WebApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
11-
<PackageReference Include="Microsoft.Identity.Web" Version="0.2.0-localbuild" />
11+
<PackageReference Include="Microsoft.Identity.Web" Version="0.2.0-preview" />
1212
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
1313
</ItemGroup>
1414

0 commit comments

Comments
 (0)