首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置Swashbuckle.AspNetCore和Oauth2

如何设置Swashbuckle.AspNetCore和Oauth2
EN

Stack Overflow用户
提问于 2019-07-04 18:19:18
回答 1查看 935关注 0票数 1

我想找出我哪里出了问题。

代码语言:javascript
复制
services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new Info { Title = "MySite API", Version = "v1" });
    options.OperationFilter<AuthorizeCheckOperationFilter>();
    options.OperationFilter<AddSwaggerHeadersOperationFilter>();
    options.AddSecurityDefinition("oauth2", new OAuth2Scheme
    {
        Type = "oauth2",
        Flow = "implicit",
        AuthorizationUrl = "authorization url",
        TokenUrl = "token url",
        Scopes = new Dictionary<string, string>()
        {
            { "scope", "Scope" }
        }
    });
});


//Configure Method
app.UseSwagger();

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "MySite API V1");
    options.OAuthClientId("MyClientId");
    options.OAuthAppName("Swagger Api Calls");
    //c.RoutePrefix = string.Empty;
});

//AuthorizeCheckOperationFilter
internal class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.TryGetMethodInfo(out var methodInfo))
        {
            var attributes = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true);
            if (attributes.OfType<AuthorizeAttribute>().Any())
            {
                operation.Responses.Add("401", new Response { Description = "Unauthorized" });
                operation.Responses.Add("403", new Response { Description = "Forbidden" });

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

//Extra field
internal class AddSwaggerHeadersOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
            operation.Parameters = new List<IParameter>();

        operation.Parameters.Add(new NonBodyParameter
        {
            Name = "SomeField",
            In = "header",
            Type = "string",
            Required = true,
            Default = "some value"
        });
    }
}

现在,当我打开“傲慢”页面时,我得到了“授权”按钮,我点击了该按钮,当我在那里填写详细信息时,我会被重定向到我的身份网站,该网站将我登录,然后直接重定向到swagger。然后昂首阔步地说授权,好像一切都很好。

然后,我尝试使用一个API,它要求传递Bearer令牌,而它没有传递它。我没有看到它的标题和我的日志从身份网站,没有任何传递。

知道为什么或者如何解决这个问题吗?我使用的是Swashbuckle.AspNetCore 4.1软件包。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-05 07:22:49

您可以添加DocumentFilter

代码语言:javascript
复制
public class SecurityRequirementsDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument document, DocumentFilterContext context)
    {
        document.Security = new List<IDictionary<string, IEnumerable<string>>>()
        {
            new Dictionary<string, IEnumerable<string>>()
            {
                { "oauth2", new string[]{ "openid", "profile", "email" } },
            }
        };
    }
}

然后在AddSwaggerGen函数中注册过滤器:

代码语言:javascript
复制
options.DocumentFilter<SecurityRequirementsDocumentFilter>();

参考资料:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/603#issuecomment-368487641

我使用您的代码示例进行测试,它的工作情况与预期的一样:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56892775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档