c# - 如何在 Swagger-ui 进行的 oauth2/token 调用中发送 "audience"字段?

标签 c# oauth-2.0 swagger swagger-ui swagger-2.0

我正在使用 Swagger-UI 和 Swashbuckle v5.6 来记录 Auth0 (OAuth2) 安全的 .NET Web API。
我一直在尝试将 Swagger 配置为从 Auth0 服务获取 UI 中的 token 。到目前为止,我已经设法做到了,但问题是我需要在 POST/token 请求的正文中发送“受众”字段,我正在努力从 SwaggerConfig.cs 找出如何做到这一点。

到目前为止,我的 SwaggerConfig.cs 看起来像这样:

public class SwaggerConfig
{
    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;
        string appName = "myApi";
        var audience = System.Configuration.ConfigurationManager.AppSettings["AuthAudience"];
        string tokenUrl = "somethingsomething/oauth/token";

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "myApi");
                    c.IncludeXmlComments(string.Format(@"{0}\bin\myApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));
                    c.DescribeAllEnumsAsStrings();

                    c.OAuth2("oauth2")
                        .Description("client credentials grant flow")
                        .Flow("password")
                        .TokenUrl(tokenUrl)
                        .Scopes(scopes =>
                        {
                            scopes.Add("myapi", "openid profile email address phone");
                        });

                    c.OperationFilter<AssignOperationFilters>();
                    c.DocumentFilter<SecurityRequirementsDocumentFilter>();
                })
            .EnableSwaggerUi(c =>
            {
                var clientId =  System.Configuration.ConfigurationManager.AppSettings["Auth0ApiClientId"];
                var clientSecret = System.Configuration.ConfigurationManager.AppSettings["Auth0ApiClientSecret"];

                var additionalParams = new Dictionary<string, string>{ {"audience", audience } };

                c.EnableOAuth2Support(clientId,
                                    clientSecret,
                                    appName,
                                    "tmdq",
                                    additionalQueryStringParams: additionalParams);

            });
    }
}

最佳答案

在从 6.0.0 版开始的 Swagger 中,您可以像这样使用请求拦截器:

 app.UseSwaggerUI(options =>
                {
                        options.UseRequestInterceptor("(req) => { if (req.url.endsWith('oauth/token') && req.body) req.body += '&audience=" + appsettings.Audience + "'; return req; }");

                });

关于c# - 如何在 Swagger-ui 进行的 oauth2/token 调用中发送 "audience"字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54254768/

相关文章:

c# - 运行 epstopdf 时出现问题。检查你的 TeX 安装

c# - 确定用户CPU大小的java程序?

c# - 如何在 Web 服务中获取客户端发送的 X509Certificate?

c# - 如何在 ServiceStack 中启用 swagger?

ruby-on-rails - 如何在 rails 中保护 swagger 页面?

c# - 为什么 task.Wait 不会在非 ui 线程上死锁?还是我只是幸运?

javascript - Nodemailer OAuth2 2LO 在 Heroku 部署中失败

android - 在 Firebase 中实现自定义身份验证的工作流程是什么?

android - 使用 AccountManager、Retrofit 和 Dagger 实现 OAuth2

c# - 自定义 Swagger UI ASP.NET Core Web API