Skip to content

Commit 3a98f0f

Browse files
Update web API sample to run in both external and workforce tenants (#105)
Update samples
1 parent 9ad84c9 commit 3a98f0f

4 files changed

Lines changed: 60 additions & 31 deletions

File tree

web-api/Api.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>dotnet_web_api</RootNamespace>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<PackageReference Include="Microsoft.Identity.Web" Version="2.*" />
11+
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.3.0" />
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
14+
<PackageReference Include="Microsoft.Identity.Web" Version="3.7.1" />
1115
</ItemGroup>
1216

13-
</Project>
17+
</Project>

web-api/Program.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
1-
// <ms_docref_import_types>
21
using Microsoft.AspNetCore.Authentication.JwtBearer;
3-
using Microsoft.AspNetCore.Authorization;
42
using Microsoft.Identity.Web;
5-
// </ms_docref_import_types>
63

7-
// <ms_docref_add_msal>
8-
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Configure authentication
97
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
10-
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
8+
.AddMicrosoftIdentityWebApi(options =>
9+
{
10+
builder.Configuration.Bind("AzureAd", options);
11+
options.TokenValidationParameters.NameClaimType = "name";
12+
}, options => { builder.Configuration.Bind("AzureAd", options); });
13+
14+
// Configure authorization
1115
builder.Services.AddAuthorization(config =>
1216
{
13-
config.AddPolicy("AuthZPolicy", policyBuilder =>
14-
policyBuilder.Requirements.Add(new ScopeAuthorizationRequirement() { RequiredScopesConfigurationKey = $"AzureAd:Scopes" }));
17+
config.AddPolicy("AuthZPolicy", policy =>
18+
policy.RequireRole("Forecast.Read"));
1519
});
16-
// </ms_docref_add_msal>
1720

18-
// <ms_docref_enable_authz_capabilities>
19-
WebApplication app = builder.Build();
21+
var app = builder.Build();
22+
23+
app.UseHttpsRedirection();
2024
app.UseAuthentication();
2125
app.UseAuthorization();
22-
// </ms_docref_enable_authz_capabilities>
2326

24-
var weatherSummaries = new[]
27+
var summaries = new[]
2528
{
2629
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
2730
};
2831

29-
// <ms_docref_protect_endpoint>
30-
app.MapGet("/weatherforecast", [Authorize(Policy = "AuthZPolicy")] () =>
32+
app.MapGet("/weatherforecast", () =>
3133
{
32-
var forecast = Enumerable.Range(1, 5).Select(index =>
33-
new WeatherForecast
34-
(
35-
DateTime.Now.AddDays(index),
36-
Random.Shared.Next(-20, 55),
37-
weatherSummaries[Random.Shared.Next(weatherSummaries.Length)]
38-
))
34+
var forecast = Enumerable.Range(1, 5).Select(index =>
35+
new WeatherForecast
36+
(
37+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
38+
Random.Shared.Next(-20, 55),
39+
summaries[Random.Shared.Next(summaries.Length)]
40+
))
3941
.ToArray();
4042
return forecast;
4143
})
42-
.WithName("GetWeatherForecast");
43-
// </ms_docref_protect_endpoint>
44+
.WithName("weatherForecast")
45+
.RequireAuthorization("AuthZPolicy"); // Protect this endpoint with the AuthZPolicy
4446

4547
app.Run();
4648

47-
record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
49+
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
4850
{
4951
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
50-
}
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5000",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7000",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}

web-api/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"AzureAd": {
3-
"Instance": "https://login.microsoftonline.com/",
3+
"Instance": "https://login.microsoftonline.com/", //For external tenants, use instance in the form of "https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/"
44
"TenantId": "Enter the tenant ID obtained from the Microsoft Entra admin center",
55
"ClientId": "Enter the client ID obtained from the Microsoft Entra admin center",
66
"Scopes": "Forecast.Read"
@@ -12,4 +12,4 @@
1212
}
1313
},
1414
"AllowedHosts": "*"
15-
}
15+
}

0 commit comments

Comments
 (0)