我们有一个asp.net web api应用程序,它使用swagger / swashbuckle作为api文档。使用oauth / openid-connect通过azure AD保护api。 swagger的配置在代码中完成:
var oauthParams = new Dictionary<string, string>
{
{ "resource", "https://blahblahblah/someId" }
};
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion(Version, Name);
c.UseFullTypeNameInSchemaIds();
c.OAuth2("oauth2")
.Description("OAuth2 Implicit Grant")
.Flow("implicit")
.AuthorizationUrl(
"https://login.microsoftonline.com/te/ourtenant/ourcustompolicy/oauth2/authorize")
.TokenUrl(
"https://login.microsoftonline.com/te/ourtenant/ourcustompolicy/oauth2/token");
c.OperationFilter<AssignOAuth2SecurityRequirements>();
})
.EnableSwaggerUi(c =>
{
c.EnableOAuth2Support(_applicationId, null, "http://localhost:49919/swagger/ui/o2c-html", "Swagger", " ", oauthParams);
c.BooleanValues(new[] { "0", "1" });
c.DisableValidator();
c.DocExpansion(DocExpansion.List);
});
当swashbuckle构建用于登录的auth url时,它会自动添加:&scope =
但是我需要这个:&scope = openid
我试过添加这个:
var oauthParams = new Dictionary<string, string>
{
{ "resource", "https://blahblahblah/someId" },
{ "scope", "openid" }
};
但这增加了:
&scope=&someotherparam=someothervalue&scope=openid
任何想法如何添加
&scope=openid
对于swashbuckle构造的auth url?
非常感谢
所以,找出问题所在,可以在这里找到有问题的代码:
https://github.com/swagger-api/swagger-ui/blob/2.x/dist/lib/swagger-oauth.js
这些js文件来自引用旧版UI的git子模块。
我可以在第154-158行看到我们有这个代码:
url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
url += '&realm=' + encodeURIComponent(realm);
url += '&client_id=' + encodeURIComponent(clientId);
url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
url += '&state=' + encodeURIComponent(state);
无论是否有范围,它基本上都会添加范围。这意味着您无法在additionalQueryParams字典中添加发送到EnableOAuth2Support的作用域,因为您将获得包含2个作用域查询参数的URL,即
&scope=&otherparam=otherparamvalue&scope=openid
围绕范围进行简单的长度检查可以解决问题。
我最终从web api项目中删除了swashbuckle并添加了一个名为swagger-net的不同nuget包,在这里找到:
https://www.nuget.org/packages/Swagger-Net/
这是积极维护,它解决了问题,并使用更新版本的swagger ui。配置保持完全相同,您唯一需要更改的是您的回复网址,现在是:
http://your-url/swagger/ui/oauth2-redirect-html