C
C#10mo ago
Beksy

dotnet 8 redirects [Authorize] Tagged endpoints to /Account/Login

For example request sent in swagger at /Api/Subscription Goes to /Account/Login?ReturnUrl=%2Fapi%2FSubscription And returns 404 since it doesnt exist
c#
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<VoithosDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));

builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<VoithosDbContext>()
.AddApiEndpoints();

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

builder.Services.AddHostedService<ProductFetchBackgroundService>();
builder.Services.AddScoped<ProductFetchService>();

builder.Services
.AddAutoMapperProfiles()
.AddServices()
.AddMediatR(typeof(HandlerBase))
.AddCors(options =>
{
options.AddPolicy("CorsPolicy", corsBuilder =>
{
corsBuilder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
})
.AddControllers();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapIdentityApi<User>();

app
.UseExceptionHandling()
.UseSwagger()
.UseSwaggerUI()
.UseRouting()
.UseCors("CorsPolicy")
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints => endpoints.MapControllers());


app.Run();
c#
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<VoithosDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));

builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<VoithosDbContext>()
.AddApiEndpoints();

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

builder.Services.AddHostedService<ProductFetchBackgroundService>();
builder.Services.AddScoped<ProductFetchService>();

builder.Services
.AddAutoMapperProfiles()
.AddServices()
.AddMediatR(typeof(HandlerBase))
.AddCors(options =>
{
options.AddPolicy("CorsPolicy", corsBuilder =>
{
corsBuilder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
})
.AddControllers();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapIdentityApi<User>();

app
.UseExceptionHandling()
.UseSwagger()
.UseSwaggerUI()
.UseRouting()
.UseCors("CorsPolicy")
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints => endpoints.MapControllers());


app.Run();
1 Reply
Anu6is
Anu6is10mo ago
it's probably due to you including app.MapIdentityApi<User>();
Want results from more Discord servers?
Add your server