79294599

Date: 2024-12-19 14:24:51
Score: 3
Natty:
Report link

Posting my solution in case it helps anyone else. I needed to add the bearer token as a authentication scheme like this:

builder.Services.AddAuthentication()
    .AddBearerToken(IdentityConstants.BearerScheme);

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddApiEndpoints()
    .AddDefaultUI()
    .AddDefaultTokenProviders();

I then needed to allow anonymous access to those API endpoints:

app.RegisterApiEndpoints();
app.MapIdentityApi<ApplicationUser>().AllowAnonymous();

Finally I needed to add a custom policy that accepted the bearer token and the cookie to the endpoints that were going to be accessed with the bearer token. The answer here really helped with that: Why am I redirected to Account/Login page when using WebApi and AspIdentityCore beside JwtBearer in dotnet 7?

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Growable