Search code examples
c#asp.netkestrel-http-server

Listening on HTTP and HTTPS? - C#/ASP.NET/Kestrel


Question - I need my app to force HTTPS connection (443) but if a connection request comes in on 80, to take it and accept it, though on my below code I am getting

IApplicationBuilder does not contain a definition for UseHttpRedirection" and no accessible extension method 'UseHttpRedirection' accepting first argument of type IA ApplicationBuilder could be found, and error HttpRequest does not contain a definition for port.

What am I doing wrong?

        // Always require HTTPS
        app.UseHttpsRedirection();
        //Use when requiring port 80
        app.UseWhen(context => context.Request.Port == 80, httpApp =>
        {
            httpApp.UseHttpRedirection();
        });

Solution

  • Well, there is no method UseHttpRedirection. The only provided redirect is from http to https

    What am I doing wrong?

    You can include the https redirect, that's fine. If you bind the server to port 80, that could also work with https. If you find that the middleware does not suit your use case, look into it and copy it: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/HttpsPolicy/src/HttpsRedirectionMiddleware.cs