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

编程入门 行业动态 更新时间:2024-10-20 15:59:58
本文介绍了如何在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.

在 github/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令牌终结点

本文发布于:2023-11-14 10:02:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   如何在   WebApi   Swagger   OAuth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!