diff --git a/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs b/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs
new file mode 100644
index 0000000..bfa0184
--- /dev/null
+++ b/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs
@@ -0,0 +1,19 @@
+using Microsoft.AspNetCore.Builder;
+
+namespace Falcon.SugarApi.Swagger
+{
+ ///
+ /// 应用扩展
+ ///
+ public static class ApplicationBuilderExtend
+ {
+ ///
+ /// 使用基础认证
+ ///
+ ///
+ ///
+ public static IApplicationBuilder UseSwaggerBasicAuth(this IApplicationBuilder builder) {
+ return builder.UseMiddleware();
+ }
+ }
+}
diff --git a/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs b/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs
new file mode 100644
index 0000000..3cd0129
--- /dev/null
+++ b/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs
@@ -0,0 +1,89 @@
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Falcon.SugarApi.Swagger
+{
+ ///
+ /// Swagger接口基础认证
+ ///
+ public class SwaggerBasicAuthMiddleware
+ {
+ ///
+ /// 下一步请求
+ ///
+ private readonly RequestDelegate Next;
+ ///
+ /// 配置
+ ///
+ public SwaggerOptions Options { get; private set; }
+
+ ///
+ /// 构造中间件
+ ///
+ /// 下一步
+ /// 配置
+ public SwaggerBasicAuthMiddleware(RequestDelegate next,SwaggerOptions options) {
+ Next = next;
+ this.Options = options;
+ }
+
+ ///
+ /// 执行中间件
+ ///
+ /// HttpContext
+ /// 任务
+ public virtual async Task InvokeAsync(HttpContext context) {
+ if(!this.Options.UseAuth) {
+ await ToNext(context);
+ return;
+ }
+ var pf = this.Options.Prefix.StartsWith("/") ? this.Options.Prefix : "/" + this.Options.Prefix;
+ if(!context.Request.Path.StartsWithSegments(pf)) {
+ await ToNext(context);
+ return;
+ }
+ string authHeader = context.Request.Headers["Authorization"];
+ if(authHeader == null || !authHeader.StartsWith("Basic ")) {
+ needAuth(context);
+ return;
+ }
+ var header = AuthenticationHeaderValue.Parse(authHeader);
+ if(header == null || header.Parameter == null) {
+ needAuth(context);
+ return;
+ }
+ var inBs = Convert.FromBase64String(header.Parameter);
+ var cred = Encoding.UTF8.GetString(inBs).Split(':');
+ var (un, pw) = (cred[0], cred[1]);
+ //var un = cred[0];
+ //var pw = cred[1];
+ if(un == this.Options.AuthName && pw == this.Options.AuthPassword) {
+ await ToNext(context);
+ return;
+ }
+ needAuth(context);
+ }
+
+ ///
+ /// 进入下一个中间件
+ ///
+ public virtual async Task ToNext(HttpContext context) {
+ await Next.Invoke(context).ConfigureAwait(false);
+ return;
+ }
+ ///
+ /// 需要认证
+ ///
+ public virtual void needAuth(HttpContext context) {
+ context.Response.Headers["WWW-Authenticate"] = "Basic";
+ context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
+ }
+
+ }
+}
diff --git a/Falcon.SugarApi/Swagger/SwaggerOptions.cs b/Falcon.SugarApi/Swagger/SwaggerOptions.cs
new file mode 100644
index 0000000..4eb1e85
--- /dev/null
+++ b/Falcon.SugarApi/Swagger/SwaggerOptions.cs
@@ -0,0 +1,34 @@
+namespace Falcon.SugarApi.Swagger
+{
+ ///
+ /// Swagger配置选项
+ ///
+ public class SwaggerOptions
+ {
+ ///
+ /// 是否启用Swagger
+ ///
+ public bool Enable { get; set; } = true;
+ ///
+ /// swagger路由前缀
+ ///
+ public string Prefix { get; set; } = "";
+ ///
+ /// 接口网页标题
+ ///
+ public string DocumentTitle { get; set; } = "ApiService Api接口";
+
+ ///
+ /// 是否启用认证
+ ///
+ public bool UseAuth { get; set; } = false;
+ ///
+ /// 认证用户名
+ ///
+ public string? AuthName { get; set; } = "";
+ ///
+ /// 认证用户密码
+ ///
+ public string? AuthPassword { get; set; } = "";
+ }
+}