本文介绍了带有 swashbuckle api 文档的 http 基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能知道我如何将基本身份验证与 swashbuckle api 的文档集成?
could anyone know how could i integrate basic auth with swashbuckle api's documentation?
我看到swaggerconfig文件中有一个basicAuth函数:
I saw that there's a basicAuth function in the swaggerconfig file:
c.BasicAuth("basic").Description("Basic HTTP Authentication");
我做了什么:
- 取消了上一行的注释,但没有任何改变!
有人知道我错过了什么吗?
does anyone have any idea what did i miss?
谢谢!
推荐答案
我是这样进行 httpbasic 身份验证的:
Here's how i did httpbasic authentication:
public class AddAuthorizationHeaderParameterOperationFilter: IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline();
var isAuthorized = filterPipeline
.Select(filterInfo => filterInfo.Instance)
.Any(filter => filter is IAuthorizationFilter);
var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
if (isAuthorized && !allowAnonymous)
{
operation.parameters.Add(new Parameter {
name = "Authorization",
@in = "header",
description = "access token",
required = true,
type = "string"
});
}
}
}
api的用户应在字段值中写入:basic [un:pw].tobase64.
The api's user shall write in the field value: basic [un:pw].tobase64.
参考资料:swashbuckle 的问题 326swashbuckle 问题 2
这篇关于带有 swashbuckle api 文档的 http 基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!