NET 6 WEB API ENABLE CERTIFICATE HTTPS ON AWS EC2

I have a problem, I can't generate the certificates in AWS EC2

Linux AWS

I trying execute this command in SSH - docker run --rm -p 3000:3000 -p 3001:3001 -e ASPNETCORE_HTTPS_PORT=https://+:3001 -e ASPNETCORE_ENVIRONMENT="Development" -e ASPNETCORE_URLS=https://+:3001 $MY ECR CONTAINER HERE$

i try too docker run --rm -p 3000:3000 -p 3001:3001 -e ASPNETCORE_HTTPS_PORT=https://+:3001 -e ASPNETCORE_ENVIRONMENT="Development" -e ASPNETCORE_URLS=https://+:3001 -v ASPNETCORE_Kestrel__Certificates__Default__Password=$MY PW$* -v ASPNETCORE_Kestrel__Certificates__Default__Path=%USERPROFILE%/aspnet/https/aspnetapp.pfx $MY CONTAINER$

Error on SSH

My Dockerfile

My Launch Settings

DOTNET INFO ON LINUX AWS

AWS CERTIFICATE MANAGER

it works perfectly on HTTP 80 but to unable HTTPS 443, a docker need a certificate.

what do i need to do to generate this certificate in aws linux?

Edit* warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[6 0] Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may n ot be persisted outside of the container. Protected data will be unavailable whe n container is destroyed. warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {f37427eb-3dc8-4d33-9177-92caadc2c880} ma y be persisted to storage in unencrypted form.

Upvotes: 0

Views: 952

Answers (1)

After a lot of searching find the following answers and my project is on LIVE.

1º I edited my program.cs so that it uses HTTPS Redirection and HSTS and configured the Forward Headers Follow the codes. `builder.Services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;

});`

app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json",
        "Api Documentation for MyLandingApp");
    });
app.UseHsts();
app.UseHttpsRedirection();
app.UseCors("MyLandingAppPolicy");
app.UseForwardedHeaders();

app.Use(async (context, next) =>
{
    if (context.Request.IsHttps || context.Request.Headers["X-Forwarded-Proto"] == Uri.UriSchemeHttps)
    {
        await next();
    }
    else
    {
        string queryString = context.Request.QueryString.HasValue ? context.Request.QueryString.Value : string.Empty;
        var https = "https://" + context.Request.Host + context.Request.Path + queryString;
        context.Response.Redirect(https);
    }
});

app.UseAuthentication();
app.UseAuthorization();

2º I added some stuff in my Appsettings.Json

  "https_port": 3001,

3ºI changed my DockerFile to create a self certificate and enable HTTPS on docker run . Docker File

4ª I changed the docker container execution string, removed the HTTP port that I wouldn't use anyway, I'll explain later.

docker run --rm -p 3001:3001 -e ASPNETCORE_HTTPS_PORT=https://+:3001 -e ASPNETCORE_ENVIRONMENT="Production" -e ASPNETCORE_URLS=https://+:3001 $MY CONTAINER IN ESR$

5º I configured the loudbalancer like this:

HTTP80 - Loud Balancer http80 HTTPS443 - Loud bALANCER https443 Só que tem o macete...

you need to create the target group pointing to the main server, then you will take the private IP and create a new target group

Target Group

With this you will have done the redirection and CERTIFICATE configuration for your API.

Remembering that in Listener https 443 you need a valid certificate.

Upvotes: 1

Related Questions