Configure redirect URI in Asp.NET Core Azure AD authentication
Oct 12
How can I tell my app to generate redirect URI with host that I provide? (foobar.com)
Context:
I have Server-Side Blazor Web App, generated from from recent (VS 16.7.5) template, with Azure AD (Single tenant, Work and Schools) authentication.
I use
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.8" />
I've configured accepted redirected URLs at Az Portal and etc. and I'm able to log in with Azure AD while running my app localy.
The redirect URI is sent in query params while being redirected to Azure AD site and it points to website host which is actualy foobar.azurewebsites.net.
How can I tell my app to generate redirect URI with host that I provide? (foobar.com)
The only solution I've found is relevant for former ASP.NET.
<add key="ida:RedirectUri" value="https://localhost:44326/" />
Which does not work for in my case.
1 answer
Accepted answer · original discussion
May 12
When the Web App sits behind Azure Front Door or an App Gateway, we need to configure the redirect_uri in the /authorize request to be the the Gateway/Front Door's address.
In the code below we override the "OnRedirectToIdentityProvider" event and inject the Front Door/Gateway's address. When I was trying this out I simply hardcoded the address but ideally you'd extract it from the headers that Front Door or App Gateway inject into the request.
This is the code I used when trying to authenticate my Blazor Server App (.net 5) running on an Azure App Service, Protected by Azure AD, running behind Azure Front Door.
public void ConfigureServices(IServiceCollection services)
{
// ... existing code
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(new[] { "User.Read" })
.AddInMemoryTokenCaches();
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = (context) =>
{
// Override the redirect_uri
// Ideally extract this from config
// Or context.Request.Headers["X-Forwarded-Host"]
// see: https://learn.microsoft.com/en-us/azure/frontdoor/front-door-http-headers-protocol#front-door-to-backend
context.ProtocolMessage.RedirectUri
= "https://YOUR-FRONT-DOOR-or-APP-GATEWAY/signin-oidc";
return Task.FromResult(0);
}
};
});
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
// ... existing code
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ... existing code
// Don't forget to add this ...
app.UseForwardedHeaders();
// ... existing code
}
When the code works, the "redirect_uri" param should point to your Front Door/Gateway as shown here.
Hope that helped. ❤️
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.