diff --git a/VideoAnalysis/Program.cs b/VideoAnalysis/Program.cs index 3cf4074..5e63fa5 100644 --- a/VideoAnalysis/Program.cs +++ b/VideoAnalysis/Program.cs @@ -115,6 +115,7 @@ namespace Learn.VideoAnalysis AppCommon.Services = app.Services; + app.UseMiddleware("Swagger"); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) diff --git a/VideoAnalysisCore/Common/BasicAuthMiddleware.cs b/VideoAnalysisCore/Common/BasicAuthMiddleware.cs new file mode 100644 index 0000000..4317807 --- /dev/null +++ b/VideoAnalysisCore/Common/BasicAuthMiddleware.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using OracleInternal.Secure.Network; +using System; +using System.Text; +using System.Threading.Tasks; + +namespace VideoAnalysisCore.Common +{ + public class BasicAuthMiddleware + { + private readonly RequestDelegate _next; + private readonly string _realm; + + + + public BasicAuthMiddleware(RequestDelegate next, string realm) + { + _next = next; + _realm = realm; + } + + public async Task InvokeAsync(HttpContext context) + { + if (context.Request.Path.StartsWithSegments("/swagger")) + { + string authHeader = context.Request.Headers["Authorization"]; + if (authHeader != null && authHeader.StartsWith("Basic ")) + { + var encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim(); + var decodedUsernamePassword = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword)); + var usernamePassword = decodedUsernamePassword.Split(':'); + + if (await IsAuthorized(usernamePassword[0], usernamePassword[1])) + { + await _next(context); + return; + } + } + + context.Response.Headers["WWW-Authenticate"] = $"Basic realm=\"{_realm}\""; + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + return; + } + + await _next(context); + } + + private async Task IsAuthorized(string username, string password) + { + // 在这里验证用户名和密码 + return AppCommon.Config.Admin.Account == username + && AppCommon.Config.Admin.Password == password; + } + } +} diff --git a/VideoAnalysisCore/Common/SSIMCalculator.cs b/VideoAnalysisCore/Common/SSIMCalculator.cs index db099c9..d1870b4 100644 --- a/VideoAnalysisCore/Common/SSIMCalculator.cs +++ b/VideoAnalysisCore/Common/SSIMCalculator.cs @@ -10,7 +10,7 @@ using SixLabors.ImageSharp.Processing; namespace VideoAnalysisCore.Common { /// - /// Ssim计算器 + /// ssim计算器 /// public class SSIMCalculator {