|
19 | 19 | using FileService.Service.Interfaces; |
20 | 20 | using Microsoft.Extensions.FileProviders; |
21 | 21 | using System.Security.Claims; |
| 22 | +using Microsoft.AspNetCore.DataProtection; |
22 | 23 |
|
23 | 24 | BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); |
24 | 25 |
|
|
40 | 41 |
|
41 | 42 | var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
42 | 43 | Console.WriteLine($"ASPNETCORE_ENVIRONMENT: {env}"); |
43 | | -//builder.Services.Configure<MinioConfig>(builder.Configuration.GetSection("MinioConfig")); |
44 | 44 |
|
45 | 45 | builder.Services.Configure<MongoDbSettings>(builder.Configuration.GetSection("MongoDbSettings")); |
46 | 46 | builder.Services.Configure<FfmpegSettings>(builder.Configuration.GetSection("FfmpegSettings")); |
|
84 | 84 | options.Password.RequiredUniqueChars = 1; |
85 | 85 | }); |
86 | 86 |
|
| 87 | +// Add Data Protection with persistence and encryption |
| 88 | +builder.Services.AddDataProtection() |
| 89 | + .PersistKeysToFileSystem(new DirectoryInfo("/root/.aspnet/DataProtection-Keys")) |
| 90 | + .ProtectKeysWithDpapi() // Use DPAPI for Windows compatibility |
| 91 | + .SetApplicationName("FileService"); |
| 92 | + |
| 93 | + |
| 94 | +//add authen |
87 | 95 | builder.Services.AddAuthentication(options => |
88 | 96 | { |
89 | 97 | options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
|
93 | 101 | { |
94 | 102 | options.TokenValidationParameters = new TokenValidationParameters |
95 | 103 | { |
96 | | - ValidateIssuer = true, |
97 | | - ValidateAudience = true, |
98 | | - ValidateLifetime = true, |
| 104 | + ValidateIssuer = false, |
| 105 | + ValidateAudience = false, |
| 106 | + ValidateLifetime = false, |
99 | 107 | ValidateIssuerSigningKey = true, |
100 | 108 | ValidIssuer = appSettings.Jwt.Issuer, |
101 | | - ValidAudience = appSettings.Jwt.Audience, |
| 109 | + // ValidAudience = appSettings.Jwt.Audience, |
102 | 110 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Jwt.Key)), |
103 | 111 |
|
104 | 112 | // Cấu hình claim để nhận Role |
105 | | - RoleClaimType = ClaimTypes.Role |
106 | | - |
| 113 | + // RoleClaimType = "roles" |
107 | 114 | }; |
| 115 | + options.TokenValidationParameters.RoleClaimType = "roles"; // Khớp với token |
| 116 | +}); |
| 117 | + |
| 118 | + |
| 119 | +// Thêm MemoryCache để cache claims |
| 120 | +builder.Services.AddMemoryCache(); |
| 121 | + |
| 122 | +//add author policies |
| 123 | +builder.Services.AddAuthorization(options => |
| 124 | +{ |
| 125 | + options.AddPolicy("AdminOnly", policy => policy.RequireRole("ADMIN")); |
| 126 | + options.AddPolicy("TeacherOrAdmin", policy => policy.RequireRole("TEACHER", "SYS_ADMIN")); |
| 127 | + options.AddPolicy("SysAdminOnly", policy => policy.RequireRole("SYS_ADMIN")); |
| 128 | + options.AddPolicy("OrgAdminOnly", policy => policy.RequireRole("ORG_ADMIN")); |
| 129 | + options.AddPolicy("TeacherOnly", policy => policy.RequireRole("TEACHER")); |
| 130 | + options.AddPolicy("UserOnly", policy => policy.RequireRole("USER")); |
| 131 | + options.AddPolicy("LoggedInUsers", policy => policy.RequireRole("USER", "TEACHER", "ORG_ADMIN", "SYS_ADMIN")); |
108 | 132 | }); |
109 | 133 |
|
110 | 134 | builder.Services.AddCors(options => |
|
164 | 188 | options.Limits.MaxRequestBodySize = 6L * 1024 * 1024 * 1024; // 6GB |
165 | 189 | }); |
166 | 190 |
|
| 191 | +// Configure HTTP client for MinIO with SSL handling |
| 192 | +builder.Services.AddHttpClient("MinioClient").ConfigurePrimaryHttpMessageHandler(() => |
| 193 | +{ |
| 194 | + return new HttpClientHandler |
| 195 | + { |
| 196 | + ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true // Temporary for dev/staging, remove in production |
| 197 | + }; |
| 198 | +}); |
| 199 | + |
167 | 200 |
|
168 | 201 | var app = builder.Build(); |
169 | 202 |
|
|
183 | 216 |
|
184 | 217 | app.UseHttpsRedirection(); |
185 | 218 | app.UseAuthentication(); |
| 219 | +// Middleware pipeline |
| 220 | +//app.UseRoleCheck(); |
186 | 221 | app.UseAuthorization(); |
187 | 222 |
|
188 | | -app.UseMiddleware<ExceptionMiddleware>(); |
| 223 | + |
189 | 224 | app.UseMiddleware<AuthenMiddleware>(); |
| 225 | +app.UseMiddleware<ExceptionMiddleware>(); |
| 226 | +//app.UseMiddleware<RoleCheckMiddleware>(); |
190 | 227 | app.UseMiddleware<UserContextMiddleware>(); |
191 | 228 |
|
192 | 229 | app.MapControllers(); |
|
0 commit comments