Nuget 安裝包區別app
Core2.2ui
Core3.0code
IDocumentFilter區別orm
Core2.2blog
Core3.0token
Core 2.2ip
app.UseSwagger(c => { c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value); });
Core 3.0get
app.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpReq) => { swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } }; }); });
原使用到的info改變爲OpenApiInfostring
Microsoft.OpenApi 代替 Swashbuckle.AspNetCore.Swaggerit
Core 2.2
Core 3.0
Swagger 添加 token Header區別
Core 2.2
public class AddAuthTokenHeaderParameter : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { if (operation.Parameters == null) operation.Parameters = new List<IParameter>(); var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo; //先判斷是不是匿名訪問, var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor; if (descriptor != null) { //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter); var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true); bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute); bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute); //非匿名的方法,連接中添加accesstoken值 if (!isAnonymous && isAuthorized) { operation.Parameters.Add(new NonBodyParameter() { Name = "Authorization", In = "header",//query header body path formData Type = "string", Default = "", Required = true //是否必選 }); } } } }
Core 3.0
public class AddAuthTokenHeaderParameter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters == null) { operation.Parameters = new List<OpenApiParameter>(); } var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo; //先判斷是不是匿名訪問, var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor; if (descriptor != null) { //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter); var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true); bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute); bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute); //非匿名的方法,連接中添加accesstoken值 if (!isAnonymous && isAuthorized) { operation.Parameters.Add(new OpenApiParameter() { Name = "Authorization", In= ParameterLocation.Header,//query header body path formData Description="Token驗證", Schema = new OpenApiSchema() { Type="string", }, //In = "header",//query header body path formData //Type = "string", //Default = "", Required = true //是否必選 }); } } } }