Код: Выделить всё
using System.Reflection;
using HalfbitZadanie.Extensions;
using InnoProducts.Models;
using InnoProducts.Repositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
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.AddSwaggerGen();
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();
Код: Выделить всё
[Authorize(AuthenticationSchemes = "Bearer")]
[HttpGet]
public async Task GetAllProducts()
{
return Ok(await _mediator.Send(new GetAllProductsQuery()));
}
Код: Выделить всё
No authentication handler is registered for the scheme 'Bearer'. The registered schemes are: Identity.Application, Identity.Bearer. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("Bearer",...)?
Код: Выделить всё
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme; // Set the default scheme
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
})
.AddCookie(IdentityConstants.ApplicationScheme).AddBearerToken(IdentityConstants.BearerScheme);
AddBearerToken(IdentityConstants.BearerScheme)
Конечная точка API без аутентификации работает нормально. У меня также не появился код 401.
Как это исправить и работать с JWT с помощью Identity?
Почему это не работает?
Подробнее здесь: https://stackoverflow.com/questions/793 ... -despite-c