如何解决Asp.Net Core 3x Swagger多头用于身份验证
我正在考虑基于标头系统中的旧PAT或API密钥实现一种简单的身份验证机制。最终,我希望用户在需要身份验证的请求期间提供两个标头。客户编号及其令牌。在邮递员中,它看起来就像这样:
并且已经设法实现了这一目标,很大程度上是通过适应我在这里找到的东西
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[] { }
}
}
);
结果就像这样
像这样正确地生成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-ID
是username
和API-Key
还是password
,还是我可能要考虑的另一种方法?
侧面说明:这个应用程序有点太简单了,无法考虑使用JWT和实现授权服务。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。