试图建立一个摇摇欲坠,我需要传递两个标头值。一个是不记名令牌,另一个是承租人ID。由于某些原因,我无法取回我在“身份验证”表单中输入的值。
这是我的代码,但不确定是否配置正确或Swagger无法正常工作。
internal static IServiceCollection AddSwagger(this IServiceCollection collection)
{
collection.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Title",
Version = "v1",
Description = "Description v1."
});
options.AddSecurityDefinition("Authorization", new OpenApiSecurityScheme
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT"
});
options.AddSecurityDefinition("x-client-accessId", new OpenApiSecurityScheme
{
Name = "x-client-accessId",
In = ParameterLocation.Header,
Scheme = "apiKey",
Type = SecuritySchemeType.ApiKey
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
[new OpenApiSecurityScheme
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT"
}] = new List<string>()
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
[new OpenApiSecurityScheme
{
Name = "x-client-accessId",
In = ParameterLocation.Header,
Scheme = "apiKey",
Type = SecuritySchemeType.ApiKey
}] = new List<string>()
});
});
return collection;
}
配置方法:
app.UseRouting();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger Movies Demo V1");
c.DisplayRequestDuration();
});
app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); });
答案 0 :(得分:1)
您还需要在Configure
方法中添加以下代码。
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
// specifying the Swagger JSON endpoint.
options.SwaggerEndpoint($"../swagger/{_apiVersion}/swagger.json", $"MyProject API v1");
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
});