【发布时间】:2019-02-09 11:15:11
【问题描述】:
我尝试使用 Swashbuckle 5.6.0 大摇大摆地描述我的 asp.net Web API OAuth 端点并尝试了这个解决方案:
How to show WebApi OAuth token endpoint in Swagger
我的问题是,接收访问令牌和通过刷新令牌获取新令牌的 URL 在 asp.net OAuth 授权服务器中是相同的。
由于“路径”是IDictionary<string, PathItem>这一事实,将第二个 URL 添加到 Swagger 文档路径失败。
public class AuthTokenOperation : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
// get the Token Endpoint from Config
var endpoint = Helpers.GetAppSetting("TokenEndPoint");
// Access Token
swaggerDoc.paths.Add(endpoint, new PathItem
{
post = new Operation
{
tags = new List<string> { "AccessToken" },
consumes = new string[] { "application/x-www-form-url-encoded" },
produces = new string[] { "application/json" },
parameters = new List<Parameter>
{
new Parameter
{
type = "string",
name = "username",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "password",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "grant_type",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "client_id",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "client_secret",
required = true,
@in = "formData"
}
}
}
});
// Refresh Token
swaggerDoc.paths.Add(endpoint, new PathItem
{
post = new Operation
{
tags = new List<string> { "AccessToken" },
consumes = new string[] { "application/x-www-form-url-encoded" },
produces = new string[] { "application/json" },
parameters = new List<Parameter>
{
new Parameter
{
type = "string",
name = "grant_type",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "client_id",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "client_secret",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "refresh_token",
required = true,
@in = "formData"
}
}
}
});
}
}
有没有可能描述两个指向同一个端点的api方法,只使用不同的参数?
【问题讨论】:
-
是的 - 我得到一个例外,具有该键的元素已添加到集合中:因为:
// Access TokenswaggerDoc.paths.Add(endpoint, new PathItem{`// ... ... 。 ..`}和// Refresh TokenswaggerDoc.paths.Add(endpoint, new PathItem{// ... ... ...}指向同一个端点。但我无法更改它们,因为它们是在 asp.net Identity 中实现的。 -
我看到问题路径是一个字典:github.com/domaindrivendev/Swashbuckle/blob/master/… 不允许重复键....我正在查看 gettyimages,让我们看看我们能做什么
-
对不起 - 没有帮助:(
-
我今天会测试它并让你更新
-
它现在可以工作 - 似乎有点“被黑”但它可以工作:) 我稍后会提供解决方案作为单独的答案。
标签: asp.net oauth swagger owin swashbuckle