mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-09 10:54:46 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
57
OpenAuth.WebApi/Model/AuthResponsesOperationFilter.cs
Normal file
57
OpenAuth.WebApi/Model/AuthResponsesOperationFilter.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// swagger请求的时候,如果是Identity方式,自动加授权方式
|
||||
/// </summary>
|
||||
public class AuthResponsesOperationFilter : IOperationFilter
|
||||
{
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
|
||||
public AuthResponsesOperationFilter(IOptions<AppSetting> appConfiguration)
|
||||
{
|
||||
_appConfiguration = appConfiguration;
|
||||
}
|
||||
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
if (!_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var anonymous = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
|
||||
.Union(context.MethodInfo.GetCustomAttributes(true))
|
||||
.OfType<AllowAnonymousAttribute>().Any();
|
||||
if (!anonymous)
|
||||
{
|
||||
var security = new List<OpenApiSecurityRequirement>();
|
||||
security.Add(new OpenApiSecurityRequirement {
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "oauth2"
|
||||
}
|
||||
},
|
||||
new[] { "openauthapi" }
|
||||
}
|
||||
});
|
||||
operation.Security = security;
|
||||
// operation.Security = new List<OpenApiSecurityRequirement>
|
||||
// {
|
||||
// new Dictionary<string, IEnumerable<string>> {{"oauth2", new[] { "openauthapi" } }}
|
||||
// };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user