diff --git a/FileService/FileService.Api/Controllers/FileDocumentController.cs b/FileService/FileService.Api/Controllers/FileDocumentController.cs index 76338979..8fdf4924 100644 --- a/FileService/FileService.Api/Controllers/FileDocumentController.cs +++ b/FileService/FileService.Api/Controllers/FileDocumentController.cs @@ -2,10 +2,14 @@ using FileService.Service.ApiModels.FileDocumentModels; using FileService.Service.Dtos.FileDocumentDtos; using FileService.Service.Interfaces; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace FileService.Api.Controllers { + // [Authorize] + // [Authorize(Policy = "Permission")] + // [Authorize(Roles = "ADMIN")] [Route("file/api/[controller]")] [ApiController] public class FileDocumentController : BaseApiController diff --git a/FileService/FileService.Api/Middlewares/AuthenMiddleware.cs b/FileService/FileService.Api/Middlewares/AuthenMiddleware.cs index 01def616..7ba07038 100644 --- a/FileService/FileService.Api/Middlewares/AuthenMiddleware.cs +++ b/FileService/FileService.Api/Middlewares/AuthenMiddleware.cs @@ -1,16 +1,20 @@ using FileService.Core.ApiModels; +using System.Security.Claims; namespace FileService.Api.Middlewares { public class AuthenMiddleware { private readonly RequestDelegate _next; + + public AuthenMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext httpContext) { + //lấy userId và sessionId từ claims var userId = httpContext.User.Claims.FirstOrDefault(c => c.Type == "userId")?.Value; var sessionId = httpContext.User.Claims.FirstOrDefault(c => c.Type == "sessionId")?.Value; diff --git a/FileService/FileService.Api/Program.cs b/FileService/FileService.Api/Program.cs index 651ba7c6..1759da09 100644 --- a/FileService/FileService.Api/Program.cs +++ b/FileService/FileService.Api/Program.cs @@ -18,6 +18,7 @@ using FileService.Service.Implementation; using FileService.Service.Interfaces; using Microsoft.Extensions.FileProviders; +using System.Security.Claims; BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); @@ -39,7 +40,7 @@ var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); Console.WriteLine($"ASPNETCORE_ENVIRONMENT: {env}"); -builder.Services.Configure(builder.Configuration.GetSection("MinioConfig")); +//builder.Services.Configure(builder.Configuration.GetSection("MinioConfig")); builder.Services.Configure(builder.Configuration.GetSection("MongoDbSettings")); builder.Services.Configure(builder.Configuration.GetSection("FfmpegSettings")); @@ -98,7 +99,11 @@ ValidateIssuerSigningKey = true, ValidIssuer = appSettings.Jwt.Issuer, ValidAudience = appSettings.Jwt.Audience, - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Jwt.Key)) + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Jwt.Key)), + + // Cấu hình claim để nhận Role + RoleClaimType = ClaimTypes.Role + }; }); @@ -173,12 +178,6 @@ app.UseSwaggerUI(); } -using (var scope = app.Services.CreateScope()) -{ - var minioService = scope.ServiceProvider.GetRequiredService(); - await minioService.EnsureBucketExistsAsync(); -} - //kích hoạt CORS policy app.UseCors(MyAllowSpecificOrigins); @@ -192,6 +191,5 @@ app.MapControllers(); - -await app.RunAsync(); +app.Run(); diff --git a/FileService/FileService.Api/appsettings.Development.json b/FileService/FileService.Api/appsettings.Development.json index cae09358..3bdd1c33 100644 --- a/FileService/FileService.Api/appsettings.Development.json +++ b/FileService/FileService.Api/appsettings.Development.json @@ -21,11 +21,11 @@ "OtpHoursAvailable": 24 }, "Jwt": { - "Key": "uifhjcnsqbhwbAMKXjdt123gdritocet", - "Issuer": "http://localhost:38946", - "Audience": "http://localhost:38946", + "Key": "1TjXchw5FloESb63Kc+DFhTARvpWL4jUGCwfGWxuG5SIf/1y/LgJxHnMqaF6A/ij", + "Issuer": "Code Campus", + "Audience": "Code Campus", "AccessTokenExpiresTime": 60, - "RefreshTokenExpiresTime": 3 + "RefreshTokenExpiresTime": 300 }, "Admin": { "OtpMaxAttempted": 3, diff --git a/FileService/FileService.Api/appsettings.Staging.json b/FileService/FileService.Api/appsettings.Staging.json index 27e4617d..cb14b20e 100644 --- a/FileService/FileService.Api/appsettings.Staging.json +++ b/FileService/FileService.Api/appsettings.Staging.json @@ -21,9 +21,9 @@ "OtpHoursAvailable": 24 }, "Jwt": { - "Key": "uifhjcnsqbhwbAMKXjdt123gdritocet", - "Issuer": "http://localhost:38946", - "Audience": "http://localhost:38946", + "Key": "1TjXchw5FloESb63Kc+DFhTARvpWL4jUGCwfGWxuGSSIf/1y/LgJxHnMqaF6A/ij", + "Issuer": "Code Campus", + "Audience": "Code Campus", "AccessTokenExpiresTime": 60, "RefreshTokenExpiresTime": 3 }, diff --git a/FileService/FileService.Service/Implementation/MinioService.cs b/FileService/FileService.Service/Implementation/MinioService.cs index df065db3..7f7a4270 100644 --- a/FileService/FileService.Service/Implementation/MinioService.cs +++ b/FileService/FileService.Service/Implementation/MinioService.cs @@ -22,25 +22,60 @@ public class MinioService : BaseService, IMinioService private readonly IMinioClient _minioClient; private readonly MinioConfig _config; - public MinioService(AppSettings appSettings, UserContext userContext) : base(appSettings, userContext) + public MinioService(IOptions options, AppSettings appSettings, UserContext userContext) : base(appSettings, userContext) { - _config = appSettings.MinioConfig ?? throw new ArgumentNullException(nameof(appSettings.MinioConfig)); + _config = options.Value ?? throw new ArgumentNullException(nameof(options.Value)); - _minioClient = new MinioClient() - .WithEndpoint(_config.Endpoint, _config.Port) - .WithCredentials(_config.AccessKey, _config.SecretKey) - .WithSSL(_config.Secure) - .Build(); + Console.WriteLine($"MinioConfig: Endpoint={_config.Endpoint}, Port={_config.Port}, AccessKey length={_config.AccessKey?.Length ?? 0}, Secure={_config.Secure}"); + + if (string.IsNullOrWhiteSpace(_config.Endpoint) || _config.Port <= 0 || string.IsNullOrWhiteSpace(_config.AccessKey) || string.IsNullOrWhiteSpace(_config.SecretKey)) + { + throw new InvalidOperationException("Minio configuration is invalid or incomplete."); + } + try + { + Console.WriteLine("Attempting to build MinioClient..."); + _minioClient = new MinioClient() + .WithEndpoint(_config.Endpoint, _config.Port) + .WithCredentials(_config.AccessKey, _config.SecretKey) + .WithSSL(_config.Secure) + .Build(); + if (_minioClient == null) + { + throw new InvalidOperationException("Failed to initialize MinioClient."); + } + // Kiểm tra kết nối thực tế + Console.WriteLine("Testing MinioClient connection..."); + var buckets = _minioClient.ListBucketsAsync().GetAwaiter().GetResult(); // Kiểm tra nhanh + Console.WriteLine("MinioClient initialized and connection tested successfully."); + } + catch (MinioException ex) + { + Console.WriteLine($"MinioException: {ex.Message}"); + throw new InvalidOperationException($"Minio initialization failed: {ex.Message}", ex); + } + catch (Exception ex) + { + Console.WriteLine($"Unexpected error: {ex.Message}"); + throw new InvalidOperationException($"Failed to initialize MinioClient: {ex.Message}", ex); + } } public async Task EnsureBucketExistsAsync() { - var bucketExistsArgs = new BucketExistsArgs().WithBucket(_config.BucketName); + if (_minioClient == null) + { + throw new InvalidOperationException("MinioClient is not initialized."); + } + if (string.IsNullOrWhiteSpace(_config.BucketName)) { throw new InvalidOperationException("Bucket name is not configured."); } + var bucketExistsArgs = new BucketExistsArgs().WithBucket(_config.BucketName); + bool found = await _minioClient.BucketExistsAsync(bucketExistsArgs); + if (!found) { var makeBucketArgs = new MakeBucketArgs().WithBucket(_config.BucketName);