I am using Swagger-UI with Swashbuckle v5.6 to document an Auth0 (OAuth2) secured .NET Web API. I've been trying to configure Swagger to obtain a token in the UI from Auth0 service. So far I've managed to do that, but the problem is that I need to send in the POST /token request's body the "audience" field, and I am struggling to find out how to do that from SwaggerConfig.cs.
So far my SwaggerConfig.cs looks like this :
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);
});
}
}
In Swagger starting in version 6.0.0 you can use Request Interceptor like this:
app.UseSwaggerUI(options =>
{
options.UseRequestInterceptor("(req) => { if (req.url.endsWith('oauth/token') && req.body) req.body += '&audience=" + appsettings.Audience + "'; return req; }");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With