如何在Swagger中显示WebApi OAuth令牌终结点 [英] How to show WebApi OAuth token endpoint in Swagger

查看:294
本文介绍了如何在Swagger中显示WebApi OAuth令牌终结点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a new Web Api project, added Asp.Net Identity and configured OAuth like so:

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

这一切工作正常,我可以调用/Token端点并获取承载令牌.

This all works fine, I can call the /Token endpoint and get a bearer token back.

问题是我认为这在Swagger中是无法发现的,因为它不在控制器上,因此没有为其生成xml文档.

The problem is that this is not discoverable in Swagger I assume because it's not on a controller and therefore has no xml documentation generated for it.

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

Does anyone know of a way to display this login endpoint in my Swagger docs?

谢谢.

此外,我应该说Swagger文档正在与我所有的控制器一起使用,只是我缺少这种明显的方法-如何登录.

Also, i should've said that the Swagger documentation is working with all my controllers, it's just that I'm missing this one obvious method - how to login.

推荐答案

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

ApiExplorer won't be automatically generating any info for your endpoint so you'll need to add a custom DocumentFilter in order to manually describe the token endpoint.

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>();
});

这篇关于如何在Swagger中显示WebApi OAuth令牌终结点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆