47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
|
|
using Microsoft.OpenApi.Any;
|
|||
|
|
using Microsoft.OpenApi.Models;
|
|||
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace YBDevice.NApi
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// swagger ui 增加header
|
|||
|
|
/// </summary>
|
|||
|
|
public class AddAuthTokenHeaderParameter : IOperationFilter
|
|||
|
|
{
|
|||
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
|||
|
|
{
|
|||
|
|
var filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors;
|
|||
|
|
//检查是否存在自定义的过滤器ApiAuthorizeFilter,如果有说明需要token
|
|||
|
|
var isAuthorized = filterDescriptors.Select(filterInfo => filterInfo.Filter).Any(filter => filter is ApiAuthorizeFilter);
|
|||
|
|
if (isAuthorized)
|
|||
|
|
{
|
|||
|
|
if (operation.Parameters == null)
|
|||
|
|
{
|
|||
|
|
operation.Parameters = new List<OpenApiParameter>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
operation.Parameters.Add(new OpenApiParameter
|
|||
|
|
{
|
|||
|
|
Name = "token",
|
|||
|
|
In = ParameterLocation.Header,
|
|||
|
|
Description = "请输入token值",
|
|||
|
|
Required = true,
|
|||
|
|
Schema = new OpenApiSchema
|
|||
|
|
{
|
|||
|
|
Type = "string",
|
|||
|
|
Default = new OpenApiString(""),
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
//operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
|
|||
|
|
//operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|