.NET 9 is finally out and I was playing around with Blazor. I was setting up Authentication in a .NET 9 Blazor Web App. The authentication is configured with AzureAD, and locally everything was working fine. The application was running on HTTPS and the redirect_uri was HTTPS too.
When the application was deployed to Azure, the Authentication was failing, because the redirect_uri was HTTP. In Azure AD App Registration I configured it with HTTPS (HTTP is allowed only when using localhost). The application was running inside a Linux Container in an Azure Web App.
In order for redirect_uri to be HTTPS, I had to do the following:
1. Enable UseForwardedHeaders
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
// Other service registrations
WebApplication app = builder.Build();
// Note: Forwarded Headers Middleware should run before other middleware.
// This ordering ensures that the middleware relying on forwarded headers information can consume the header values for processing.
// Forwarded Headers Middleware can run after diagnostics and error handling, but it MUST BE RUN before calling UseHsts
app.UseForwardedHeaders();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
// Other middleware
app.Run();
2. Add the following app setting in Azure (More: Forward the scheme for Linux and non-IIS reverse proxies)
{
"name": "ASPNETCORE_FORWARDEDHEADERS_ENABLED",
"value": "true",
"slotSetting": false
}
And that did it.
Hope this helps.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment