在ASP.NET Core中的Swagger 5中的AuthorizeCheckOperationFilter

huangapple go评论65阅读模式
英文:

AuthorizeCheckOperationFilter in Swagger 5 in ASP.NET Core

问题

在较早版本的Swagger中,我一直在使用这种方法:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
            .Union(context.MethodInfo.GetCustomAttributes(true))
            .OfType<AuthorizeAttribute>();
        if (authAttributes.Any())
        {
            operation.Responses.Add("401", new Response { Description = "Unauthorized" });                    

            operation.Security = new List<IDictionary<string, IEnumerable<string>>>
            {
                new Dictionary<string, IEnumerable<string>>
                {
                    { "oauth2", new[] { "myscope" } }
                }
            };
        }
    }
}

然而,现在接口已经改变成了这样:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        throw new NotImplementedException();
    }
}

如何转换这段代码呢? 在ASP.NET Core中的Swagger 5中的AuthorizeCheckOperationFilter

非常感谢!
Gunnar

英文:

I've been using this method in an older version of Swagger:

        public class AuthorizeCheckOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
                .Union(context.MethodInfo.GetCustomAttributes(true))
                .OfType&lt;AuthorizeAttribute&gt;();
            if (authAttributes.Any())
            {
                operation.Responses.Add(&quot;401&quot;, new Response { Description = &quot;Unauthorized&quot; });                    

                operation.Security = new List&lt;IDictionary&lt;string, IEnumerable&lt;string&gt;&gt;&gt;
                                     {
                                         new Dictionary&lt;string, IEnumerable&lt;string&gt;&gt;
                                         {
                                             { &quot;oauth2&quot;, new[] { &quot;myscope&quot; } }
                                         }
                                     };
            }

      
        }
    }

However, now the interface has changed into this:

    public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        throw new NotImplementedException();
    }
}

How do I convert the code? 在ASP.NET Core中的Swagger 5中的AuthorizeCheckOperationFilter

Many thanks!
Gunnar

答案1

得分: 0

你可以参考官方GitHub文档进行如下更改:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var authAttributes = context.MethodInfo
        .GetCustomAttributes(true)
        .OfType<AuthorizeAttribute>()
        .Select(attr => attr.Policy)
        .Distinct();
        if (authAttributes.Any())
        {
            operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
            var oAuthScheme = new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
            };

            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    [oAuthScheme] = authAttributes.ToList()
                }
            };               
        }
    }
}
英文:

You could refer to the official github doc to change like below:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var authAttributes = context.MethodInfo
        .GetCustomAttributes(true)
        .OfType&lt;AuthorizeAttribute&gt;()
        .Select(attr =&gt; attr.Policy)
        .Distinct();
        if (authAttributes.Any())
        {
            operation.Responses.Add(&quot;401&quot;, new OpenApiResponse { Description = &quot;Unauthorized&quot; });
            var oAuthScheme = new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = &quot;oauth2&quot; }
            };

            operation.Security = new List&lt;OpenApiSecurityRequirement&gt;
            {
                new OpenApiSecurityRequirement
                {
                    [ oAuthScheme ] = authAttributes.ToList()
                }
            };               
        }
    }
}

huangapple
  • 本文由 发表于 2020年1月4日 01:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定