Reputation: 1025
I have an application gateway configured with my wildcard certificate that I want to use to proxy myapp.azurewebsites.net (an ASP.NET core application) behind myapp.mywebsite.net/mypath.
I have an existing site running on myapp.mywebsite.net configured in the gateway, but I want just the /mypath
route to point to the app service. How can I accomplish this?
Upvotes: 0
Views: 1432
Reputation: 1025
myapp.azurewebsites.net
myapp.azurewebsites.net
. Don't add the path override, we want the /mypath
to be passed to the app service./mypath/*
mypathname
// can be whateverThis will point myapp.mywebsite.net/mypath
to the site
See here for more info.
Add the following to the very start of the Configure method. We want headers to be adjusted before all other middleware happens.
app.UseForwardedHeaders(); // Enable hostname to be derived from headers added by app gateway
app.UsePathBase("/mypath"); // Tell ASP.NET that we have a base path
See here for debugging help.
We need to tell ASP.NET to trust the gateway headers
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.AllowedHosts.Add("myapp.mywebsite.net");
options.KnownProxies.Add(IPAddress.Parse("10.my.gateway.ip"));
});
If you are using
services.AddMicrosoftIdentityWebAppAuthentication(config);
for auth, we need to override the reply url so it points to myapp.mywebsite.net/mypath/signin-oidc
instead of myapp.azurewebsites.net/signin-oidc
.
This can be done with:
if (!env.IsDevelopment())
{
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
// options.SaveTokens = true; // this saves the token for the downstream api
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = async ctxt =>
{
ctxt.ProtocolMessage.RedirectUri = "https://myapp.mywebsite.net/mypath/signin-oidc";
await Task.Yield();
}
};
});
}
We only run this in dev so that running our stuff locally does the default behaviour of filling the replyurl with localhost.
Upvotes: 1