NSwag的AspNetCoreOperationSecurityScopeProcessor将所有端点标记为需要授权

问题描述

我有这样的自定义授权方案:

services.AddAuthentication("ClientApp")
                .AddScheme<ClientAppAuthenticationOptions,ClientAppAuthenticationHandler>("ClientApp",null);

然后我具有以下NSwag OpenAPI文档配置:

services.AddOpenApiDocument((settings,provider) =>
            {
                settings.DocumentName = "openapi";
                settings.AddSecurity("ClientApp",Enumerable.Empty<string>(),new OpenApiSecurityScheme
                {
                    Type = OpenApiSecuritySchemeType.ApiKey,Description = "Authentications used for client apps,such as Mmcc.Stats.TpsMonitor",Name = "X-Auth-Token",In = OpenApiSecurityApiKeyLocation.Header
                });

                settings.OperationProcessors.Add(
                    new AspNetCoreOperationSecurityScopeProcessor("ClientApp")
                );
                // ...
            }

我已经用[AllowAnonymous][Authorize(AuthenticationSchemes = "ClientApp")]装饰了控制器中的动作,但是NSwag将我的所有端点标记为需要ReDoc UI中的ClientApp授权,而不考虑装饰器。为什么?

解决方法

我通过将代码更改为此来解决它:

settings.DocumentProcessors.Add(
                    new SecurityDefinitionAppender("ClientApp",new OpenApiSecurityScheme
                        {
                            Type = OpenApiSecuritySchemeType.ApiKey,Description = "Authentications used for client apps,such as Mmcc.Stats.TpsMonitor",Name = "X-Auth-Token",In = OpenApiSecurityApiKeyLocation.Header
                        }));
                settings.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("ClientApp"));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...