-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (126 loc) · 4.89 KB
/
Copy pathProgram.cs
File metadata and controls
146 lines (126 loc) · 4.89 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using SmartParcel.API.Data;
using System; // For InvalidOperationException
using System.Text;
using SmartParcel.API.Services.Implementations;
using SmartParcel.API.Services.Interfaces;
using System.Drawing;
using System.Runtime.InteropServices;
var builder = WebApplication.CreateBuilder(args);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
}
// ✅ 1. Configure EF Core with PostgreSQL
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// ✅ 2. Configure Response Compression (Move this before app.Build())
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
// Add this line with your other service registrations
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<ITamperHandler, TamperHandler>();
builder.Services.AddScoped<IPricingService, PricingService>();
// Add System.Drawing configuration for cross-platform support
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
}
// ✅ 2. Configure JWT Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"], // FIX: Changed to Jwt:Audience
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]
?? throw new InvalidOperationException("JWT key is missing"))),
// ✅ Matches the updated AuthController claim format
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
};
});
// ✅ 3. Configure Controllers & Swagger
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
{
Name = "Authorization",
Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
Description = "Enter 'Bearer' [space] and then your valid token."
});
c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
{
{
new Microsoft.OpenApi.Models.OpenApiSecurityScheme
{
Reference = new Microsoft.OpenApi.Models.OpenApiReference
{
Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
// ✅ 4. Enable CORS
// Define a specific policy name
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:5173", // Your React app's development URL
"http://127.0.0.1:5173") // Often useful to include both
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials(); // FIX: Added AllowCredentials for auth headers
});
options.AddPolicy("AllowAll",
builder =>
{
builder
.SetIsOriginAllowed(origin => true) // Instead of AllowAnyOrigin
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials(); // Now compatib
});
});
var app = builder.Build();
// ✅ Enable Swagger only in development
//if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// ✅ Middleware order matters!
// FIX: Use the named CORS policy
app.UseResponseCompression();
app.UseHttpsRedirection(); // Move this before CORS
app.UseCors("AllowAll");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// ✅ Apply EF Core migrations to the Render DB
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
dbContext.Database.Migrate();
}
app.Run();