我有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" });
    });
}


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

    host.Run();
}

我用这个命令:
docker run -it --rm -p 1234:80 --name voiconshop_ps voiconshop

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

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

我运行http://localhost:1234/,然后直接指向https://localhost:44339/。但是,页面返回“无法访问此站点”。
那我做错了什么?
请帮我。非常感谢。
如果需要更多信息,我将编辑此问题。

最佳答案

在您的实施中,我看到了多个问题。

  • 我认为以HTTPs运行Web应用程序/服务需要绑定(bind)SSL证书。考虑下面的代码片段,以替换代码中的.UseKestrel()。实现LoadCertificate方法以返回您的SSL证书(X509Certificate2对象)
    .UseKestrel((options => { options.Listen(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 443),
                 listenOptions =>
                 {
    
                     var certificate = LoadCertificate();
                     var httpsConnectionAdapterOptions = new AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions()
                     {
                         ClientCertificateMode = AspNetCore.Server.Kestrel.Https.ClientCertificateMode.NoCertificate,
                         SslProtocols = System.Security.Authentication.SslProtocols.Tls,
                         ServerCertificate = certificate
                     };
                     listenOptions.UseHttps(httpsConnectionAdapterOptions);
                 });
    
  • 在dockerfile中添加443端口。它看起来像

    暴露80443
  • 我们还应该在docker-compose.override.yml中将“ASPNETCORE_URLS”环境变量设置为“http://:80 /; https://:443 /”。

    环境:
  • ASPNETCORE_ENVIRONMENT =开发
  • ASPNETCORE_URLS = http://:80 /; https://.443

  • 端口:
  • “80”
  • “443”
  • 关于asp.net-core - ASP.NET Core SSL无法在Docker容器中运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49757169/

    10-13 07:02