pictureServer/PictureFilesApi/Common/SiteIdHeaderParameter.cs

44 lines
1.3 KiB
C#
Raw Normal View History

2026-03-10 09:04:34 +08:00

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
{
/// <summary>
/// swagger过滤操作
/// </summary>
public class SiteIdHeaderParameter : IOperationFilter
{
/// <summary>
/// 使用
/// </summary>
/// <param name="operation"></param>
/// <param name="context"></param>
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
{
operation.Parameters = new List<OpenApiParameter>();
}
//判断是否存在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
});
}
}
}
}