I have a web API that seems to only accept http requests and refuses all https request as they all timeout.
http://api.maxiterations.com/models?companyCode=1&start=0&count=0 -> Works
https://api.maxiterations.com/models?companyCode=1&start=0&count=0 -> Does not Works
A request to my AWS hosted API fails to connect to the application.
However, when it is an https request from the postman to the local host it works properly.
I need assistance on how to get AWS to accept an https request since it seems like my code isn't what's doing the rejection.
Code:
// https://stackoverflow.com/questions/69291182/how-to-reject-requests-in-a-net-core-api-based-on-the-values-sent-in-the-accept
// https://stackoverflow.com/questions/58231669/is-there-any-way-to-block-http-requests-made-by-postman-in-net-core
app.Use((context, next) =>
{
// if health check call come through allow it to pass
if (context.Request.Path.ToString().EndsWith('/'))
{
return next();
}
// if request is from https allow else reject if it is http
if (context.Request.IsHttps)
{
return next();
}
context.Response.StatusCode = StatusCodes.Status403Forbidden;
return context.Response.WriteAsync("HTTPS is required");
});
app.UseHealthChecks("/");
app.MapGet("/models", (string companyCode, int start = 0, int count = 0, DateTime? earliestCreationDate = null) =>
{
// Stuff happens
return enumerable.Count != 0 ? Results.Ok(enumerable) : Results.NoContent();
})
.WithName("GetModelsByCompanyId")
.WithDescription("Returns all models owned by the company using the company code").WithTags("Models").WithOpenApi();
First, I went to Microsoft's website for ASP.NET hoping that the documentation would give me a clue and there was nothing there.
Then I thought since it is on AWS, maybe I'd find a clue in the AWS deployment documenation for dotnet 8 Web Apis but there was nothing about my particular issue.
I then tried googling and stack overflow and still came up empty.
The solution was performing TLS termination on the load balancer based on the response from @FahmiNoorFiqri.
I used AI to get the steps for creating a proper load balancer and modified them to fix my existing load balancer.
P.S. By deleting, the HTTP listener, you can make it so your API will only accept HTTPS requests.