gpt4 book ai didi

asp.net-web-api - 如何在 Swagger 中显示 WebApi OAuth token 端点

转载 作者:行者123 更新时间:2023-12-03 01:15:06 24 4
gpt4 key购买 nike

我创建了一个新的 Web Api 项目,添加了 Asp.Net Identity 并配置了 OAuth,如下所示:

OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};

一切正常,我可以调用/Token 端点并获取不记名 token 。

问题是,我认为在 Swagger 中无法发现它,因为它不在 Controller 上,因此没有为其生成 xml 文档。

有人知道在我的 Swagger 文档中显示此登录端点的方法吗?

谢谢。

另外,我应该说 Swagger 文档适用于我的所有 Controller ,只是我缺少这个明显的方法 - 如何登录。

最佳答案

ApiExplorer 不会自动为您的端点生成任何信息,因此您需要添加自定义 DocumentFilter 才能手动描述 token 端点。

https://github.com/domaindrivendev/Swashbuckle/issues/332 有一个这样的例子:

class AuthTokenOperation : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
swaggerDoc.paths.Add("/auth/token", new PathItem
{
post = new Operation
{
tags = new List<string> { "Auth" },
consumes = new List<string>
{
"application/x-www-form-urlencoded"
},
parameters = new List<Parameter> {
new Parameter
{
type = "string",
name = "grant_type",
required = true,
@in = "formData"
},
new Parameter
{
type = "string",
name = "username",
required = false,
@in = "formData"
},
new Parameter
{
type = "string",
name = "password",
required = false,
@in = "formData"
}
}
}
});
}
}

httpConfig.EnableSwagger(c =>
{
c.DocumentFilter<AuthTokenOperation>();
});

关于asp.net-web-api - 如何在 Swagger 中显示 WebApi OAuth token 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com