Asp.Net Core 3x Swagger多头用于身份验证

问题描述

我正在考虑基于标头系统中的旧PAT或API密钥实现一种简单的身份验证机制。最终,我希望用户在需要身份验证的请求期间提供两个标头。客户编号及其令牌。在邮递员中,它看起来就像这样:

Postman Request

并且已经设法实现了这一目标,很大程度上是通过适应我在这里找到的东西

https://stackoverflow.com/a/61365691/4087918

我的定义如下

c.AddSecurityDefinition("API-Key",new OpenApiSecurityScheme
{
    Description = "API Key required to access the api.",In = ParameterLocation.Header,Name = "API-Key",Type = SecuritySchemeType.ApiKey
});

c.AddSecurityDefinition("Client-ID",new OpenApiSecurityScheme
{
    Description = "Client ID required to access the api.",Name = "Client-ID",Type = SecuritySchemeType.ApiKey
});

c.OperationFilter<ApiKeyHeaderFilter>();

和我的过滤器有点像这样:

operation.Security.Add(
    new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Name = "API-Key",Type = SecuritySchemeType.ApiKey,Reference = new OpenApiReference 
                {
                    Type = ReferenceType.SecurityScheme,Id = "API-Key"
                }
            },new string[] { }
        }
    }

operation.Security.Add(
    new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Name = "Client-ID",Id = "Client-ID"
                }
            },new string[] { }
        }
    }
);

结果就像这样

Swagger Authorization Modal

像这样正确地生成curl请求

curl -X GET "https://localhost:44394/Configuration/application-key" -H "accept: text/plain" -H "API-Key: token" -H "Client-ID: client-id"

重要的是,我不想通过用[FromHeader]参数修饰每个授权的端点或使用SwaggerHeader属性来定义对Client-ID的要求,我只想通过Swagger访问这两个标头用户界面,以便用户可以验证其请求。

最后我的问题。我已经决定需要两个值来认证请求,但这是否是正确的处理方式,我的意思是约定等。或者,我应该遵循一条基本的身份验证路线,并提供Client-IDusernameAPI-Key还是password,还是我可能要考虑的另一种方法?

侧面说明:这个应用程序有点太简单了,无法考虑使用JWT和实现授权服务。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)