How to show WebApi OAuth token endpoint in Swagger

前端 未结 2 1539
日久生厌
日久生厌 2020-12-14 18:30

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

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpo         


        
2条回答
  •  生来不讨喜
    2020-12-14 18:58

    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.

    There's an example of this at 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 { "Auth" },
                    consumes = new List
                    {
                        "application/x-www-form-urlencoded"
                    },
                    parameters = new List {
                        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();
    });
    

提交回复
热议问题