本文介绍了与swashbuckle API文档HTTP基本认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
会有人知道我怎么能与集成的API swashbuckle的文档基本身份验证?
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");
我做了什么:
- 取消注释的previous线,但什么都没有改变!
没有任何人有任何想法,我错过了什么?
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的用户应在字段值写:基本[联合国:密码]。.tobase64
The api's user shall write in the field value: basic [un:pw].tobase64.
参考:
这篇关于与swashbuckle API文档HTTP基本认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!