简体   繁体   English

使用客户端凭据流进行Swashbuckle OAuth2授权

[英]Swashbuckle OAuth2 Authorization with Client Credentials Flow

I use Swashbuckle to documentation of WebAPI controllers. 我使用Swashbuckle来记录WebAPI控制器。 Also I use OAuth2 with Client Credentials Flow. 我还使用OAuth2和Client Credentials Flow。 So to authorize I need to pass client_id and client_secret . 所以要授权我需要传递client_idclient_secret

I have following code: 我有以下代码:

config.EnableSwagger(c => {
    c.SingleApiVersion("v1", "My API");
    c.OAuth2("oauth2")
        .Flow("application")
        .TokenUrl("/oauth2/token");
    c.OperationFilter<AssignOAuthSecurityRequirements>();
})
.EnableSwaggerUi(c => {
    c.EnableOAuth2Support(clientId: "clientIdValue", clientSecret:"clientSecretValue", "", "");
    c.CustomAsset("index", Assembly.GetExecutingAssembly(), "WebAPI.Swagger.UI.index.html");
});

Authorization works fine but my client_id and client_secret values are hardcoded(clientIdValue, clientSecretValue). 授权工作正常,但我的client_idclient_secret值是硬编码的(clientIdValue,clientSecretValue)。 How can I add possibility to input that values by user in this dialog? 如何在此对话框中添加用户输入值的可能性? Can anyone help me? 谁能帮我?

在此输入图像描述

Please let me know if I need to post code of AssignOAuthSecurityRequirements too. 如果我还需要发布AssignOAuthSecurityRequirements的代码,请告诉我。 Thanks all in advance 在此先感谢所有人

Not sure exactly what went wrong in your code, maybe the lack of scope definitions. 不确定您的代码到底出了什么问题,可能缺少范围定义。

I've done it successfully with ASP.NET Core and the current version of Swashbuckle.AspNetCore ( https://github.com/domaindrivendev/Swashbuckle.AspNetCore ) 我已成功使用ASP.NET Core和当前版本的Swashbuckle.AspNetCore( https://github.com/domaindrivendev/Swashbuckle.AspNetCore

The client credentials flow is referred to as "application" so, in your Startup.cs file, you need to configure Swagger as follows: 客户端凭据流称为“应用程序”,因此,在Startup.cs文件中,您需要按如下方式配置Swagger:

        services.AddSwaggerGen(c => {

            //other configs...

            c.AddSecurityDefinition("oauth2", new OAuth2Scheme {
                Type = "oauth2",
                Flow = "application",
                TokenUrl = "<token_endpoint_url>",
                Scopes = new Dictionary<string, string>
                {
                    { "first-scope", "First scope description" },
                    { "second-scope", "Second scope description" }
                    //define as many scopes as you want...
                }
            });
        });

The TokenUrl parameter must point to a valid OAuth 2.0 compliant Token endpoint (checkout http://docs.identityserver.io/en/release/endpoints/token.html for a sample on how the endpoint should behave/look like). TokenUrl参数必须指向有效的OAuth 2.0兼容Token端点(checkout http://docs.identityserver.io/en/release/endpoints/token.html ,以获取有关端点应如何表现/看起来像的示例)。 Both absolute and relative URLs worked in my tests. 绝对和相对URL都在我的测试中起作用。

After that, the authorization dialog should look like bellow: 之后,授权对话框应如下所示:

授权弹出窗口

  • Please note, that you need to select at least one scope before the authorize button actually submits anything (the oauth component should be changed to add a disclaimer IMHO). 请注意,在授权按钮实际提交任何内容之前,您需要选择至少一个范围(应更改oauth组件以添加免责声明恕我直言)。

No additional configuration was required in the SwaggerUI section. SwaggerUI部分不需要其他配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM