pictureServer/PictureFilesApi/Common/SiteIdHeaderParameter.cs

44 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
});
}
}
}
}