4

I have Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    var options = new RewriteOptions().AddRedirectToHttps(301, 44339);
    app.UseRewriter(options);

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    var localizedOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(localizedOptions.Value);

    app.UseIdentity();

     app.UseMvc(routes =>
    {
        routes.MapRoute(
            "LocalizedDefault",
            "{lang:lang}/{controller=Home}/{action=Index}/{id?}"
        );

        routes.MapRoute(
            "default",
            "{*catchall}",
            new { controller = "Home", action = "RedirectToDefaultLanguage", lang = "vi" });
    });
}

and

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel().UseUrls("https://*:443")
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.Run();
}

I used this cmd:

docker run -it --rm -p 1234:80 --name voiconshop_ps voiconshop

Here is ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
f752d350823b        voiconshop          "dotnet VoiConShop.d…"   About a minute ago   Up 59 seconds       0.0.0.0:1234->80/tcp    voiconshop_ps

And this is dockerfile

FROM microsoft/aspnetcore:1.1
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "VoiConShop.dll"]

I run http://localhost:1234/ then direct to https://localhost:44339/. However, the page return "This site can’t be reached". So what did I do wrongly? Please help me. Thanks you so much. If need more information I will edit this question.

1 Answer 1

2

I see multiple issues in your implementation.

  1. I think running web app/service in HTTPs requires SSL certificate to be bound. Consider the below code snippet to replace .UseKestrel() in your code. Implement LoadCertificate method to return your SSL Certificate(X509Certificate2 object)

     .UseKestrel((options => { options.Listen(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 443),
                  listenOptions =>
                  {
                      var httpsConnectionAdapterOptions = new AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions()
                      {
                          ClientCertificateMode = AspNetCore.Server.Kestrel.Https.ClientCertificateMode.NoCertificate,
                          SslProtocols = System.Security.Authentication.SslProtocols.Tls,
                          ServerCertificate = LoadCertificate()
                      };
                      listenOptions.UseHttps(httpsConnectionAdapterOptions);
                  }); 
    
  2. Add 443 port in dockerfile. It should look like

    Expose 80 443

  3. We should also set "ASPNETCORE_URLS" Envionment Variable with "http://:80/;https://:443/" in docker-compose.override.yml.

    environment:

    • ASPNETCORE_ENVIRONMENT=Development
    • ASPNETCORE_URLS=http://:80/;https://.443

    ports:

    • "80"
    • "443"
Sign up to request clarification or add additional context in comments.

2 Comments

Could you post LoadCertificate() to here?
You can just use "return new X509Certificate2(certFilePath, certPassword)" in the method with certificate file(.pfx) path and its password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.