无法使用 Azure Entra ID 登录 Google Cloud Run 来运行和部署 ASP.NET Core 6 Web 应用程序

问题描述 投票:0回答:1

TL;DR 版本:我无法使用 Azure AD 登录 Google Cloud Run 来运行/部署 ASP.NET Core 6 Web 应用程序。应用程序重定向到 http 版本而不是 https

我有一个 ASP.NET Core 6 Web 应用程序,我正在尝试将其部署到 Google Cloud Run。大多数教程都是基本的 Web 应用程序,但就我而言,解决方案中有多个项目。我未能在现有项目中执行此操作,因此我创建了一个名为

ComplexApp
的示例项目(灵感来自 https://github.com/dotnet/dotnet-docker/blob/main/samples/complexapp/README.md dotnet docker 存储库)。该应用程序没有什么复杂的,它只引用了 Web 解决方案中的核心项目。

My solution

我按照以下步骤在 Google Cloud Run 上构建并运行该应用程序:

  1. 为项目创建docker文件
  2. 在 Google Cloud Artifact 注册表中创建了存储库
  3. 使用 Google Cloud Build 通过以下命令构建 Docker 容器:
    gcloud builds submit --tag {region}-docker.pkg.dev/{projectId}/{repositoryName}/complex-app-image:tag1
  4. 使用从 Google Cloud 网站运行的 Google Cloud 来部署容器(创建新服务 -> 从现有容器映像部署)

一切都很好,我可以访问我的测试 Web 应用程序:

Example website 1

我没有提到的一件重要的事情是,由于我在 Cloud Run 中使用 Docker 运行这个应用程序时遇到了很多问题,所以我阅读了 Cloud Run 文档 (https://cloud.google.com/run/docs/quickstarts/build -and-deploy/deploy-dotnet-service),在 C# 示例中,建议使用此代码块来运行应用程序。

var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
var url = $"http://0.0.0.0:{port}";

app.Run(url);

我并没有完全理解我何时进行了此更改,但它在示例中,并且还引用了 Container Runtime Contract 页面,并且下面的描述(图片)指出了 0.0.0.0 和端口 8080。

Container runtime contract image

有趣的部分来了。我必须在此项目中使用 Microsoft Entra ID(又名 Azure AD)登录。因此,要求是只有我组织中的人员才能看到该 Web 应用程序。因此,下一步,我按照 Microsoft 官方文档 (https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-web-app-dotnet-core-sign-in) 进行操作在项目中实现Azure AD登录。

我遇到的第一个问题是,如果我在 Program.cs 文件中使用

var url = $"http://0.0.0.0:{port}";
(不是 https),我在本地会收到错误:

enter image description here

System.Exception: An error was encountered while handling the remote login.
 ---> System.Exception: Correlation failed.
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我有点预料到了这一点,因为它是 http 版本,并且可能不适用于重定向 URI。

如果我使用

var url = $"https://0.0.0.0:{port}";
(与https),一切都很好。我在 Chrome 的私人窗口中输入了“https://localhost:8080”。应用程序将我重定向到我公司的 Azure AD 登录页面,我已输入凭据并重定向到应用程序。

enter image description here

这适用于 https,因为我已将 https://localhost:8080/signin-oidc 和 https://0.0.0.0:8080/signin-oidc 输入到 Azure 中应用程序注册的 Web 重定向 URI 部分.

enter image description here

现在,如果我构建一个映像并将此版本部署到 Google Cloud,我会收到以下 Cloud Build 错误:

The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information..

来自日志:

Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
Default STARTUP TCP probe failed 1 time consecutively for container "complex-app-aad-image-1" on port 8080. The instance was not started.

如果我使用

var url = $"http://0.0.0.0:{port}";
(http 版本)进行部署,Cloud Run 会成功部署项目,但是当我单击链接时,出现此错误:

enter image description here

我单击的 URL 已在我的应用程序注册的重定向 URI 部分中:

enter image description here

我无法添加此 URL 的 http 版本,Azure 门户不允许我这样做。此外,我想从我访问的链接中利用 Google Cloud Run 的默认 HTTPS 证书。

正如我之前所解释的,如果没有 Azure AD 登录功能,我将使用 http 版本部署应用程序,而 Cloud Run 会重定向到 https。如何通过 Azure AD 登录实现此目的?

Startup.cs文件:https://gist.github.com/alpersilistre/090b5ac6e58d54e0fd189dd3e4018806

Dockerfile:https://gist.github.com/alpersilistre/64cc89cbb8e9f810492f8a59ab25accb

docker asp.net-core google-cloud-platform google-cloud-run microsoft-entra-id
1个回答
0
投票

根据@AlperSilistre 的评论发布为社区:

该网址实际上已经存在,为 https://..../singin-oidc,但您的评论鼓励了我,并给了我进一步研究使用 docker 在 asp.net core 上进行反向代理的想法。我找到了这篇文章,似乎有一个关于此的问题:github.com/dotnet/aspnetcore/issues/22572我已经设法通过使用

ForwardedHeaders
(通过使用
UseForwardedHeaders
中间件)解决它)在挖掘更多之后。

© www.soinside.com 2019 - 2024. All rights reserved.