swagger 如何给使用权表头值增加默认值

798qvoo8  于 2023-01-30  发布在  其他
关注(0)|答案(1)|浏览(245)

我使用的是asp.netcore 3.1和openapi 3.0.1
我已经使用以下代码向我的API添加了授权:

services.AddSwaggerGen(setupAction =>
        {
            setupAction.SwaggerDoc("APIs_Documentation", new OpenApiInfo
            {
                Title = "Project APIs",
                Version = "1"
            });

            setupAction.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
            {
                Type = SecuritySchemeType.Http,
                Scheme = "bearer",
                Description = "Enter token here",
                Name = "Authorization",
                @In = ParameterLocation.Header,

            });
            setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference
                        {
                        Type = ReferenceType.SecurityScheme,
                        Id = "oauth2",

                        }
                    },new List<string>()}
        });
        });

有没有办法为下一个图像中弹出的授权对话框的值字段设置默认值?

qij5mzcb

qij5mzcb1#

如果您正在寻找添加默认头的方法,可以在C#中这样做:

app.UseSwagger()
.UseSwaggerUI(c =>

{
    c.Interceptors = new InterceptorFunctions
    {
        RequestInterceptorFunction = "function (req) { req.headers['MyCustomHeader'] = 'CustomValue'; return req; }"
    };
}

);
或者想要将其添加到Swagger UI:related question

相关问题