I have a .NET 8 Blazor solution. In the server project, I added some Minimal APIs protected with individual accounts (just using the Visual Studio template).
For example:
public static void MapClientEndpoints (this IEndpointRouteBuilder routes){ var group = routes.MapGroup("/api/Client").WithTags(nameof(Client)); group.MapGet("/", async (HypnoContext db) => { return await db.Client.ToListAsync(); }) .RequireAuthorization() .WithName("GetAllClients") .WithOpenApi();}Also, in the server side, I configure the IHttpClientFactory like this:
builder.Services .AddScoped(sp => sp .GetRequiredService<IHttpClientFactory>() .CreateClient("ServerAPI")) .AddHttpClient("ServerAPI", (provider, client) => {#if DEBUG client.BaseAddress = new Uri("https://localhost:7241");#else client.BaseAddress = new Uri("https://myrealurl");#endif });In the client project, I added the same configuration for IHttpClientFactory. If I call from a page the API via HttpClient, the call successfully retrieves the data.
Now, I want to use different configuration for each environment. For this reason, I created different appsettings files (appsettings.Development.json, appsettings.Production.json, ...).
To load those settings, I added this code:
IHostbuilder.Environmentuilder.Environment;builder.Configuration .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);Adding this code, the calls to the APIs are not authenticated any more. In the appsettings.json, there is no configuration for the IHttpClientFactory.
What can I do to fix this issue?