Код: Выделить всё
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerWithJwtAuth();
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme; // Set the default scheme
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
}).AddCookie(IdentityConstants.ApplicationScheme).AddBearerToken(IdentityConstants.BearerScheme);
builder.Services.AddIdentityCore().AddEntityFrameworkStores().AddApiEndpoints();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddDbContext(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddRequestValidations();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.ApplyMigrations();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapIdentityApi();
app.Run();
Код: Выделить всё
[HttpGet]
public async Task GetAllProducts()
{
return Ok(await _mediator.Send(new GetAllProductsQuery()));
}
[Authorize]
[HttpGet("{id}")]
public async Task GetProductById(int id)
{
var result = await _mediator.Send(new GetProductByIdQuery { Id = id });
// return result != null ? Ok(result) : NotFound();
return Ok(result);
}
return
Код: Выделить всё
Code Details
404
Undocumented
Error: response status is 404
Response headers
Я не получаю код 401.
При использовании авторизации Swagger он возвращается та же ошибка. Я могу войти и зарегистрироваться через Identity, и все работает нормально.
Что не так с конфигурацией?
Подробнее здесь: https://stackoverflow.com/questions/793 ... pected-401