带有 swashbuckle api 文档的 http 基本身份验证身份验证、基本、文档、swashbuckle

2023-09-07 14:37:39 作者:窃鬼君.

谁能知道我如何将基本身份验证与 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