如何在Swagger中显示WebApi OAuth令牌端点

Rus*_*uss 16 asp.net-web-api asp.net-identity swagger-ui swashbuckle

我创建了一个新的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
};
Run Code Online (Sandbox Code Playgroud)

这一切都很好,我可以调用/ Token端点并获得一个持有者令牌.

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

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

谢谢.

另外,我应该说Swagger文档正在与我的所有控制器一起工作,这只是我错过了一个明显的方法 - 如何登录.

Rua*_*ose 29

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

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>();
});
Run Code Online (Sandbox Code Playgroud)


kur*_*l94 7

如果有人想知道如何向此操作添加响应正文,这里是更新的 Ruaidhri 代码:

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/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"
                    }
                },
                responses = new Dictionary<string, Response>()
                {
                    {
                        "200",
                        new Response {schema = schemaRegistry.GetOrRegister(typeof(OAuthTokenResponse))}
                    }
                }
            }
        });
    }
}

class OAuthTokenResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("expires_in")]
    public long ExpiresIn { get; set; }

    [JsonProperty("userName")]
    public string Username { get; set; }

    [JsonProperty(".issued")]
    public DateTime Issued { get; set; }

    [JsonProperty(".expires")]
    public DateTime Expires { get; set; }
}
Run Code Online (Sandbox Code Playgroud)