OpenAuth.Net/OpenAuth.WebApi/Model/AuthResponsesOperationFilter.cs
2021-01-14 23:35:54 +08:00

59 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.Linq;
using Infrastructure;
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" } }}
// };
}
}
}
}