我正在尝试将 Swashbuckle 5.0.x 与 OAuth2 一起使用。我想使用 OAuth2 的资源所有者密码凭据授予。我基本上只想先请求一个 token ,然后在每个请求中包含这个 token (例如不需要范围)。
任何人都可以帮忙吗?我必须如何配置 swagger/swashbuckle?
最佳答案
谢谢@邓肯。您的回答几乎解决了我的问题,但要使其适用于最新的 Swashbuckle 版本我不得不像这样改变它
$('#explore').off();
$('#explore').click(function () {
var key = $('#input_apiKey')[0].value;
var credentials = key.split(':'); //username:password expected
$.ajax({
url: "yourAuthEndpoint",
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.swaggerUi.api.clientAuthorizations.add('Authorization', new SwaggerClient.ApiKeyAuthorization('Authorization', bearerToken, 'header'));
window.swaggerUi.api.clientAuthorizations.remove("api_key");
alert("Login successfull");
},
error: function (xhr, ajaxoptions, thrownerror) {
alert("Login failed!");
}
});
});
关于Swagger/Swashbuckle : OAuth2 with Resource Owner Password Credentials Grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275499/