skip to Main Content

I have program.cs:

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<User>().AddEntityFrameworkStores<ApplicationDbContext>().AddApiEndpoints();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();

builder.Services.AddDbContext<ApplicationDbContext>(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<User>();

app.Run();

and then i have an endpoint:

 [Authorize(AuthenticationSchemes = "Bearer")]
    [HttpGet]
    public async Task<IActionResult> GetAllProducts()
    {
        return Ok(await _mediator.Send(new GetAllProductsQuery()));
    }

when i call it i get:

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",...)?

despite the lines:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme; // Set the default scheme
    options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
})
    .AddCookie(IdentityConstants.ApplicationScheme).AddBearerToken(IdentityConstants.BearerScheme);

and this:

AddBearerToken(IdentityConstants.BearerScheme)

The Api endopoint without Auth works just fine. I also didnt get 401 code.
How to fix it and work with JWT using Identity?
Why it doesnt work?

2

Answers


  1. Change attribute to:

    [Authorize(AuthenticationSchemes = IdentityConstants.ApplicationScheme)]
    
    Login or Signup to reply.
  2. Change attribute to:

    [Authorize(AuthenticationSchemes = IdentityConstants.ApplicationScheme)]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search