【问题标题】:Unable to configure ASP.NET HTTPS endpoint in Linux docker on Windows无法在 Windows 上的 Linux docker 中配置 ASP.NET HTTPS 端点
【发布时间】:2020-07-26 13:19:06
【问题描述】:

我在尝试调试指向 Visual Studio 中“生产”ASPNETCORE_ENVIRONMENT 的 docker 容器时遇到问题。 “开发”环境运行良好。我正在尝试针对生产容器进行调试,因为每个环境的不同 appsettings 文件存在问题。

这是我的错误:

无法配置 HTTPS 端点。未指定服务器证书,默认开发人员证书找不到或已过期。 要生成开发人员证书,请运行“dotnet dev-certs https”。要信任证书(仅限 Windows 和 macOS),请运行“dotnet dev-certs https --trust”。 有关配置 HTTPS 的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=848054

我浏览了几篇文章,但在针对生产进行调试时似乎没有任何效果。当我从 launchSettings.json 中删除 https 时,该站点根本无法运行。

https://github.com/dotnet/dotnet-docker/blob/master/samples/host-aspnetcore-https.md

Unable to configure ASP.NET HTTPS endpoint in Windows docker container

https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.1&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos

环境:

Windows 10
Linux 容器
ASP.NET Core 3.1

启动设置:

"Docker": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Production",
    "ASPNETCORE_URLS": "https://+:443;http://+:80",
  },
  "httpPort": 51934,
  "useSSL": true,
  "sslPort": 44349
}

Docker 文件

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.2-bionic AS base
WORKDIR /app
EXPOSE 80
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y tzdata
RUN apt install -y --allow-unauthenticated \
    libc6-dev \
    libgdiplus \
    libx11-dev \
 && rm -rf /var/lib/apt/lists/*

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /src
COPY src .
RUN dotnet restore "ExampleApp.Web/ExampleApp.Web.csproj"
COPY . .
WORKDIR "/src/ExampleApp.Web"
RUN dotnet dev-certs https --trust
RUN dotnet build "ExampleApp.Web.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ExampleApp.Web.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENV ASPNETCORE_ENVIRONMENT Production

ENTRYPOINT ["dotnet", "ExampleApp.Web.dll", "--environment=Production"]

【问题讨论】:

    标签: asp.net visual-studio docker asp.net-core


    【解决方案1】:

    一些选项

    选项 1: 在应用程序内部,像这样配置,它应该可以工作

    WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
                    .UseUrls(YourWebAppUrls)
                    .UseKestrel()
                    .ConfigureKestrel(options =>
                    {
                        options.ListenAnyIP(51934);  // whatever your port
                    })
                    .UseIIS()
    

    选项 2:

    在您的构建任务中,您可以将其添加到命令行中,参见here

    关闭您的浏览器,以免它们缓存证书,因为这会导致其他问题。

    在命令行运行这个

    dotnet dev-certs https --clean
    

    然后运行

    dotnet dev-certs https -t
    

    选项 3: Self signed Cert


    选项 4: 从here运行这些命令

    dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p { password here }
    dotnet dev-certs https --trust
    

    使用 Windows 容器的 Windows 生成证书并配置本地机器:

    dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
    dotnet dev-certs https --trust
    

    运行 Container 镜像,Core 配置为 HTTPS:

    docker pull mcr.microsoft.com/dotnet/core/samples:aspnetapp
    docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="password" -e ASPNETCORE_Kestrel__Certificates__Default__Path=\https\aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:C:\https\ mcr.microsoft.com/dotnet/core/samples:aspnetapp
    

    【讨论】:

    • 我的环境是docker。这些适用于本地计算机上的 IIS。
    • 试一试,看看他们都有Linux机器的引用。
    • 开发工作。只有当环境指向生产环境时。
    • hmm .. 你有反向代理或产品中不同的东西吗?设置是什么样的。
    • 你在数字海洋吗?我从 ASP 核心 2.2 发布了一些配置,尝试一下,我列出的前几个选项在我在那里托管时有效。不过,在 Windows 容器或 ASP 托管方面,Digital Ocean 的支持很糟糕。
    猜你喜欢
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2022-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多