asp.net – .NET 6 – minimal web app for permanent redirect on Azure App Service on Linux

I have a subdomain which must redirect any route to a main domain root:

  1. http(s)://subdomain.example.com/ => https://example.com
  2. http(s)://subdomain.example.com/{*anything} => https://example.com

So, I have created a minimal .NET 6 web app:

using System.Net;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpsRedirection(options =>
{
    options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
    options.HttpsPort = 443;
});

WebApplication app = builder.Build();

app.Urls.Add("http://+");
app.Urls.Add("https://+");

app.UseHttpsRedirection();

app.MapGet("/", () => Results.Redirect("https://example.com", true, true));
app.MapGet("/{*_}", (string _) => Results.Redirect("https://example.com", true, true));

app.Run();

When I run it locally, everything works:

Then I created App Service (Linux) in Azure for which:

  • I set up my custom domain subdomain.example.com
  • I added App Service Managed certificate for my custom subdomain
  • HTTPS only is ON

Looks good so far. But when I deploy my app and run it, 2 problems:

  1. opening https://mycustomapp.azurewebsites.net shows me This site has been reported as unsafe (!)
  2. when open http(s)://subdomain.example.com I get application error

And here’s what I see in the logs:


2022-06-03T03:43:22.170878337Z ASP .NETCore Version: 6.0.3

2022-06-03T03:43:22.170881637Z Note: Any data outside ‘/home’ is not persisted

2022-06-03T03:43:22.462209593Z Running oryx create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -defaultAppFilePath /defaulthome/hostingstart/hostingstart.dll -bindPort 8080 -userStartupCommand ‘dotnet PermanentRedirectMinimal.dll’

2022-06-03T03:43:22.553430350Z Cound not find build manifest file at ‘/home/site/wwwroot/oryx-manifest.toml’

2022-06-03T03:43:22.553445850Z Could not find operation ID in manifest. Generating an operation id…

2022-06-03T03:43:22.553843654Z Build Operation ID: [guid]

2022-06-03T03:43:23.236717018Z

2022-06-03T03:43:23.237393225Z Agent extension

2022-06-03T03:43:23.237407225Z Before if loop >> DotNet Runtime

2022-06-03T03:43:23.277833149Z DotNet Runtime 6.0Writing output script to ‘/opt/startup/startup.sh’

2022-06-03T03:43:23.358910299Z Running user provided startup command…

2022-06-03T03:43:25.470314040Z Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.

2022-06-03T03:43:25.470359841Z To generate a developer certificate run ‘dotnet dev-certs https’. To trust the certificate (Windows and macOS only) run ‘dotnet dev-certs https –trust’.

2022-06-03T03:43:25.470366541Z For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

2022-06-03T03:43:25.470370941Z at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)

2022-06-03T03:43:25.470375241Z at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)

2022-06-03T03:43:25.470379341Z at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)

2022-06-03T03:43:25.470383541Z at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context, CancellationToken cancellationToken)

2022-06-03T03:43:25.470387541Z at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)

2022-06-03T03:43:25.470402041Z at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)

2022-06-03T03:43:25.470405941Z at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

2022-06-03T03:43:25.470409541Z at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)

2022-06-03T03:43:25.470413041Z at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

2022-06-03T03:43:25.470416641Z at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

2022-06-03T03:43:25.470426442Z at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)

2022-06-03T03:43:25.470430142Z at Microsoft.AspNetCore.Builder.WebApplication.Run(String url)

2022-06-03T03:43:25.472178560Z at Program.$(String[] args) in e:…PermanentRedirectMinimalProgram.cs:line 22 [ ==> where app.Run() is ]

2022-06-03T03:43:41.244915770Z Could not open output /home/logs/dumps/coredump.d26e6752ab2d.36.1654227802: 2 No such file or directory

2022-06-03T03:43:41.253765645Z Gathering state for process 36 dotnet

2022-06-03T03:43:41.253793945Z Crashing thread 00000024 signal 00000006

2022-06-03T03:43:41.253799545Z Writing minidump with heap to file /home/logs/dumps/coredump.d26e6752ab2d.36.1654227802

2022-06-03T03:43:41.762563322Z /opt/startup/startup.sh:line 10:36 Aborted (core dumped) dotnet PermanentRedirectMinimal.dll

2022-06-03T03:43:42.474Z INFO – Waiting for response to warmup request for container annuaire_0_4715a3e4. Elapsed time = 19.6895811 sec

2022-06-03T03:43:42.508Z ERROR – Container blah_0_4715a3e4 for site blah has exited, failing site start

2022-06-03T03:43:42.511Z ERROR – Container blah_0_4715a3e4 didn’t respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.

2022-06-03T03:43:42.520Z INFO – Stopping site blah because it failed during startup.


Leave a Comment