using Google.Apis.Discovery; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using System.Collections.Generic; using System.Linq; namespace LongHuParkApi.Common { /// /// swagger过滤操作 /// public class SiteIdHeaderParameter : IOperationFilter { /// /// 使用 /// /// /// public void Apply(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters == null) { operation.Parameters = new List(); } //判断是否存在AllowAnonymousAttribute属性 var count = context.MethodInfo.CustomAttributes.Where(c=>c.AttributeType.Name == "AllowAnonymousAttribute").Count(); if (count == 0) { //不存在属性,则需要token验证 operation.Parameters.Add(new OpenApiParameter { Name = "token", In = ParameterLocation.Header, Required = false }); } } } }