-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (81 loc) · 3.52 KB
/
Program.cs
File metadata and controls
96 lines (81 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.Extensions.Logging.AzureAppServices;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi.Models;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
// Add Azure stream log service
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
});
builder.Logging.AddFilter((provider, category, logLevel) =>
{
return provider!.ToLower().Contains("woodgroveapi");
});
ConfigurationSection entraExternalIdCustomAuthTokenSettings = (ConfigurationSection)builder.Configuration.GetSection("EntraExternalIdCustomAuthToken");
// Reference:
// There is an issue validating the first party token with
// https://learn.microsoft.com/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer
// https://learn.microsoft.com/en-us/aspnet/core/security/authentication/configure-jwt-bearer-authentication
builder.Services.AddAuthentication()
.AddJwtBearer("EntraExternalIdCustomAuthToken", jwtOptions =>
{
jwtOptions.MetadataAddress = entraExternalIdCustomAuthTokenSettings["MetadataAddress"]!;
jwtOptions.Audience = entraExternalIdCustomAuthTokenSettings["Audience"];
jwtOptions.IncludeErrorDetails = true;
jwtOptions.MapInboundClaims = false;
jwtOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true
};
jwtOptions.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
// Validate the authorized party (the app who issued the token)
string? clientappId = context?.Principal?.Claims.FirstOrDefault(x => x.Type == "azp" && x.Value == "99045fe1-7639-4a75-9d4a-577b6ca3810f")?.Value;
if (clientappId == null)
{
context!.Fail("Invalid azp claim value");
}
return Task.CompletedTask;
}
};
});
ConfigurationSection entraExternalIdUserToken = (ConfigurationSection)builder.Configuration.GetSection("EntraExternalIdUserToken");
builder.Services.AddAuthentication()
.AddJwtBearer("EntraExternalIdUserToken", jwtOptions =>
{
jwtOptions.MetadataAddress = entraExternalIdUserToken["MetadataAddress"]!;
jwtOptions.Audience = entraExternalIdUserToken["Audience"];
jwtOptions.IncludeErrorDetails = true;
jwtOptions.MapInboundClaims = false;
jwtOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true
};
});
// Add in memory cache
builder.Services.AddMemoryCache();
builder.Services.AddControllers();
// The following line enables Application Insights telemetry collection.
builder.Services.AddApplicationInsightsTelemetry();
var app = builder.Build();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();