using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
namespace learun.webapi
{
///
/// 版 本 Learun-ADMS-Core 力软管理后台
/// Copyright (c) 2019-present 力软信息技术(苏州)有限公司
/// 创建人:tobin
/// 日 期:2020.04.17
/// 描 述:给api添加权限验证信息参数
///
public class AddAuthTokenHeaderParameter : IOperationFilter
{
///
/// 给接口添加隐性参数
///
/// api操作接口
/// 上下文
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
//var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
//先判断是否是匿名访问,
var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
if (descriptor != null)
{
var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
//非匿名的方法,链接中添加accesstoken值
if (!isAnonymous)
{
if (operation.Parameters == null) operation.Parameters = new List();
operation.Parameters.Add(new OpenApiParameter()
{
Name = "token",
Description = "登录密钥",
In = ParameterLocation.Header,
Schema = new OpenApiSchema() { Type = "string" },
Required = true //是否必选
});
}
}
}
}
}