Swagger/Swashbuckle:具有资源所有者密码凭据授予的 OAuth2

编程入门 行业动态 更新时间:2024-10-27 17:25:00
本文介绍了Swagger/Swashbuckle:具有资源所有者密码凭据授予的 OAuth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将 Swashbuckle 5.0.x 与 OAuth2 结合使用.我想使用 OAuth2 的资源所有者密码凭据授予.我基本上只想先请求一个令牌,然后在每个请求中包含这个令牌(例如不需要范围).

I'm trying to use Swashbuckle 5.0.x with OAuth2. I want to use OAuth2's Resource Owner Password Credentials Grant. I basically only want to ask for a token first and include this token in each request (e.g. no need for scopes).

有人可以帮忙吗?我必须如何配置 swagger/swashbuckle?

Can anyone help with this? How do I have to configure swagger/swashbuckle?

推荐答案

好的,我是这样解决的:

OK, I solved it like this:

为 swagger 添加一个 JavaScript 完成处理程序:

Add a JavaScript completion-handler to swagger:

config .EnableSwagger(c => { //do stuff }) .EnableSwaggerUi(c => { c.InjectJavaScript(typeof(Startup).Assembly, "MyNamespace.SwaggerExtensions.onComplete.js"); });

从 API_KEY 文本框中获取用户名:密码:

Take username:password from the API_KEY textbox:

$('#input_apiKey').change(function () { var key = $('#input_apiKey')[0].value; var credentials = key.split(':'); //username:password expected $.ajax({ url: "myURL", type: "post", contenttype: 'x-www-form-urlencoded', data: "grant_type=password&username=" + credentials[0] + "&password=" + credentials[1], success: function (response) { var bearerToken = 'Bearer ' + response.access_token; window.authorizations.add('key', new ApiKeyAuthorization('Authorization', bearerToken, 'header')); }, error: function (xhr, ajaxoptions, thrownerror) { alert("Login failed!"); } }); });

更多推荐

Swagger/Swashbuckle:具有资源所有者密码凭据授予的 OAuth2

本文发布于:2023-11-04 21:54:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭据   所有者   密码   资源   Swagger

发布评论

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

>www.elefans.com

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