Это мой текущий код program.cs:
Код: Выделить всё
using Microsoft.OpenApi.Models;
using SA.Entities;
using SA.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure(builder.Configuration.GetSection("MongoDB"));
builder.Services.AddSingleton();
// Add services to the container.
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
{
Description = " ApiKey must appear in header",
Type = SecuritySchemeType.ApiKey,
Name = "XApiKey",
In = ParameterLocation.Header,
Scheme = "ApiKeyScheme"
});
var key = new OpenApiSecurityScheme()
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ApiKey"
},
In = ParameterLocation.Header
};
var requirement = new OpenApiSecurityRequirement
{
{ key, new List()}
};
c.AddSecurityRequirement(requirement);
});
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");;
app.Run();
[img]https:/ /i.sstatic.net/jdpwM.png[/img]
должно быть 401 — Неавторизованный, как в этом руководстве.
Подробнее здесь: https://stackoverflow.com/questions/757 ... th-swagger
Мобильная версия