mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2026-02-19 06:36:29 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
54
OpenAuth.WebApi/Model/GlobalHttpHeaderOperationFilter.cs
Normal file
54
OpenAuth.WebApi/Model/GlobalHttpHeaderOperationFilter.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
// <copyright file="GlobalHttpHeaderOperationFilter.cs" company="openauth.me">
|
||||
// Copyright (c) 2019 openauth.me. All rights reserved.
|
||||
// </copyright>
|
||||
// <author>www.cnblogs.com/yubaolee</author>
|
||||
// <date>2019-01-05</date>
|
||||
// <summary>在swagger界面加上http header</summary>
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using OpenAuth.App;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace OpenAuth.WebApi.Model
|
||||
{
|
||||
public class GlobalHttpHeaderOperationFilter : IOperationFilter
|
||||
{
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
|
||||
public GlobalHttpHeaderOperationFilter(IOptions<AppSetting> appConfiguration)
|
||||
{
|
||||
_appConfiguration = appConfiguration;
|
||||
}
|
||||
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
//如果是Identity认证方式,不需要界面添加x-token得输入框
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
return;
|
||||
|
||||
if (operation.Parameters == null)
|
||||
{
|
||||
operation.Parameters = new List<OpenApiParameter>();
|
||||
}
|
||||
|
||||
var actionAttrs = context.ApiDescription.ActionDescriptor.EndpointMetadata;
|
||||
var isAnony = actionAttrs.Any(a => a.GetType() == typeof(AllowAnonymousAttribute));
|
||||
|
||||
//不是匿名,则添加默认的X-Token
|
||||
if (!isAnony)
|
||||
{
|
||||
operation.Parameters.Add(new OpenApiParameter
|
||||
{
|
||||
Name = Define.TOKEN_NAME,
|
||||
In = ParameterLocation.Header,
|
||||
Description = "当前登录用户登录token",
|
||||
Required = false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user