diff --git a/SwaggerStudy.Models/Student.cs b/SwaggerStudy.Models/Student.cs
index 0770d49..6b431dd 100644
--- a/SwaggerStudy.Models/Student.cs
+++ b/SwaggerStudy.Models/Student.cs
@@ -2,6 +2,9 @@
namespace SwaggerStudy.Models
{
+ ///
+ /// 瀛︾敓
+ ///
public class Student
{
public int Id { get; set; }
diff --git a/SwaggerStudy/Program.cs b/SwaggerStudy/Program.cs
index dd40f72..e172ed2 100644
--- a/SwaggerStudy/Program.cs
+++ b/SwaggerStudy/Program.cs
@@ -22,7 +22,7 @@ namespace SwaggerStudy
.ConfigureWebHostDefaults(webBuilder =>
{
//测试 Swagger 设置
- webBuilder.UseStartup();
+ //webBuilder.UseStartup();
//不使用 Swagger
//webBuilder.UseStartup();
@@ -38,6 +38,9 @@ namespace SwaggerStudy
//使用Newtonsoft替代默认System.Text.Json序列化库
//webBuilder.UseStartup();
+
+ //Jwt认证
+ webBuilder.UseStartup();
});
}
}
diff --git a/SwaggerStudy/Startup04.cs b/SwaggerStudy/Startup04.cs
new file mode 100644
index 0000000..d4606f6
--- /dev/null
+++ b/SwaggerStudy/Startup04.cs
@@ -0,0 +1,148 @@
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+
+using Swashbuckle;
+using Swashbuckle.AspNetCore.Swagger;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using Swashbuckle.AspNetCore.SwaggerUI;
+
+
+using SwaggerStudy.Services;
+using Microsoft.OpenApi.Models;
+using System.Reflection;
+
+namespace SwaggerStudy
+{
+ ///
+ /// 娣诲姞 Jwt璁よ瘉
+ ///
+ public class Startup04
+ {
+ public Startup04(IConfiguration configuration)
+ {
+ Configuration = configuration;
+ }
+
+ public IConfiguration Configuration { get; }
+
+ public void ConfigureServices(IServiceCollection services)
+ {
+ services
+ .AddControllers()
+ //閰嶇疆System.Text.Json閫夐」
+ .AddJsonOptions(jsonOption =>
+ {
+ //鏁撮綈鎵撳嵃锛氬帇缂╂牸寮(涓琛)杩樻槸鏁撮綈鏍煎紡(鎹㈣)
+ jsonOption.JsonSerializerOptions.WriteIndented = true;
+ //鍏佽(蹇界暐)瀵硅薄鎴栨暟鎹湯灏惧浣欑殑閫楀彿
+ jsonOption.JsonSerializerOptions.AllowTrailingCommas = true;
+ //鑷畾涔夋椂闂存牸寮忚浆鎹㈠櫒
+ jsonOption.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
+ //杞箟瀛楃涓茬紪鐮佸櫒锛氱洿鎺ユ樉绀轰腑鏂
+ jsonOption.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All);
+ });
+
+ //娉ㄥ唽Swagger鐢熸垚鍣紝瀹氫箟涓涓垨澶氫釜 Swagger 鏂囨。
+ services.AddSwaggerGen(setup =>
+ {
+ #region 瀹氫箟Swagger鏂囨。
+ //name鍙傛暟鍗充负SwaggerUI涓璖waggerEndpoint鏂规硶鍙傛暟涓殑{documentName}
+ //涓よ呭繀椤讳繚鎸佷竴鑷达紝鍚﹀垯寮傚父
+ setup.SwaggerDoc(name: "v1", new OpenApiInfo { Title = "Swagger瀛︿範", Version = "绗竴鐗" });
+ #endregion
+
+ #region 鍖呭惈xml娉ㄩ噴
+ //var xmlCommentFiles = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, $"SwaggerStudy*.xml", System.IO.SearchOption.TopDirectoryOnly);
+ var xmlCommentFiles = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}*.xml", System.IO.SearchOption.TopDirectoryOnly);
+ foreach (var xmlFile in xmlCommentFiles)
+ {
+ //includeControllerXmlComments鍙傛暟锛氭槸鍚﹀惎鐢ㄦ帶鍒跺櫒涓婄殑xml娉ㄩ噴
+ setup.IncludeXmlComments(filePath: xmlFile, includeControllerXmlComments: true);
+ }
+ #endregion
+
+ #region 鏀剧疆鎺ュ彛Auth鎺堟潈鎸夐挳
+ setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
+ {
+ Description = "璇疯緭鍏ュ甫鏈塀earer鐨凾oken锛欱earer {Token}",
+
+ //jwt榛樿鐨勫弬鏁板悕绉
+ Name = "Authorization",
+
+ //jwt榛樿瀛樻斁 Authorization 淇℃伅鐨勪綅缃細姝ゅ涓鸿姹傚ご涓
+ In = ParameterLocation.Header,
+
+ //楠岃瘉绫诲瀷锛氭澶勪娇鐢ˋpi Key
+ Type = SecuritySchemeType.ApiKey
+ });
+ #endregion
+
+ #region 鎸囧畾鏂规搴旂敤鑼冨洿
+ setup.AddSecurityRequirement(new OpenApiSecurityRequirement
+ {
+ {
+ new OpenApiSecurityScheme
+ {
+ Reference = new OpenApiReference
+ {
+ Id = "Bearer",
+ Type = ReferenceType.SecurityScheme
+ }
+ },
+ new List()
+ }
+ });
+ #endregion
+ });
+
+ services.AddTransient();
+ }
+
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+ {
+ if (env.IsDevelopment())
+ {
+ app.UseDeveloperExceptionPage();
+ }
+
+ app.UseStaticFiles();
+
+ //鍚敤 Swagger 涓棿浠
+ app.UseSwagger(setup =>
+ {
+
+ });
+
+ //鍚敤 SwaggerUI 涓棿浠
+ app.UseSwaggerUI(setup =>
+ {
+ #region 娣诲姞Swagger JSON绔偣
+ //蹇呴』鍖呭惈{documentName}鍗虫槸SwaggerDoc鐨刵ame鍙傛暟.
+ //榛樿涓/swagger/{documentName}/swagger.json
+ setup.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger瀛︿範绗竴鐗");
+ #endregion
+
+ setup.InjectJavascript("/swagger/ui/zh_cn.js");
+ });
+
+ app.UseRouting();
+
+ app.UseAuthorization();
+
+ app.UseEndpoints(endpoints =>
+ {
+ endpoints.MapControllers();
+ });
+ }
+ }
+}
diff --git a/SwaggerStudy/SwaggerStudy.csproj b/SwaggerStudy/SwaggerStudy.csproj
index fb313ce..fee1ebb 100644
--- a/SwaggerStudy/SwaggerStudy.csproj
+++ b/SwaggerStudy/SwaggerStudy.csproj
@@ -18,4 +18,8 @@
+
+
+
+