-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (65 loc) · 2.48 KB
/
Copy pathProgram.cs
File metadata and controls
75 lines (65 loc) · 2.48 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
using DotNetEnv;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Env.Load();
var builder = WebApplication.CreateBuilder(args);
// Load environment variables for Azure AD
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(
jwtBearerOptions => { }, // Configure JwtBearerOptions if needed
microsoftIdentityOptions =>
{
// Set Microsoft Identity options using environment variables
microsoftIdentityOptions.Instance = Environment.GetEnvironmentVariable("AZURE_AD_INSTANCE")!;
microsoftIdentityOptions.Domain = Environment.GetEnvironmentVariable("AZURE_AD_DOMAIN")!;
microsoftIdentityOptions.TenantId = Environment.GetEnvironmentVariable("AZURE_AD_TENANT_ID")!;
microsoftIdentityOptions.ClientId = Environment.GetEnvironmentVariable("AZURE_AD_CLIENT_ID")!;
});
// Register controllers
builder.Services.AddControllers();
// Configure Swagger to use Bearer authentication
builder.Services.AddSwaggerGen(options =>
{
// Add the JWT Bearer token authentication to Swagger UI
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Please enter JWT Bearer token"
});
// Make the Bearer authentication required for all operations
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
// Enable Swagger and Swagger UI
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.OAuthClientId(Environment.GetEnvironmentVariable("AZURE_AD_CLIENT_ID")); // Optional, for OAuth2
c.OAuthClientSecret(Environment.GetEnvironmentVariable("AZURE_AD_CLIENT_SECRET")); // Optional
});
}
app.UseAuthentication(); // Enable authentication middleware
app.UseAuthorization(); // Enable authorization middleware
app.MapControllers(); // Map API controllers
app.Run();