【发布时间】:2021-06-30 20:06:52
【问题描述】:
Ciao, 我正在开发一个包含这三个组件的 Web 应用程序:
- IdentityServer:使用 IdentityServer4
- Web API:使用 ASP.NET Core 5
- Web 应用程序:使用 ASP.NET Blazor(服务器端)。
我在 Windows 中使用 Docker for Desktop 版本 20.10.5,构建 55c4c88。
我会将我的应用程序组件作为 Docker 容器进行调试和部署。我为每个组件添加了一个 Dockerfile,并添加了对 docker-compose 的解决方案支持。
每个 Dockerfile 都会公开端口 80 和 443。
...
EXPOSE 80
EXPOSE 443
...
我的 docker-compose 文件如下:
version: '3.4'
services:
webapp:
image: ${DOCKER_REGISTRY-}webapp
ports:
- "44382:443"
build:
context: .
dockerfile: WebApp/Dockerfile
depends_on:
- identityserver
- webapi
networks:
- internal
webapi:
image: ${DOCKER_REGISTRY-}webapi
ports:
- "44305:443"
build:
context: .
dockerfile: WebApi/Dockerfile
depends_on:
- identityserver
networks:
- internal
identityserver:
image: ${DOCKER_REGISTRY-}identityserver
ports:
- "443:443"
build:
context: .
dockerfile: IdentityServer/Dockerfile
networks:
- internal
networks:
internal:
我已经使用这两个包配置了带有 IdentityServer 的 Web App:
<PackageReference Include="IdentityModel" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.2" />
还有这些配置:
public void ConfigureServices(IServiceCollection services)
{
// Adding my dependencies...
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
// Temporarly I've disabled HTTPS but it doesn't let work the project
options.RequireHttpsMetadata = false;
options.Authority = Configuration["OpenID:Authority"];
options.ClientId = Configuration["OpenID:ClientId"];
options.ClientSecret = Configuration["OpenID:ClientSecret"];
options.ResponseType = "code";
options.Scope.Add("WebApi");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.UsePkce = true;
options.Events = new OpenIdConnectEvents
{
OnAccessDenied = context =>
{
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
});
services.AddMvcCore(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
}
我使用 docker-compose 通过 Visual Studio 2019 启动应用程序。当我尝试启动应用程序时,出现如下错误:
SocketException:连接被拒绝 System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError 错误,CancellationToken cancelToken)
HttpRequestException:连接被拒绝 (身份服务器:443) System.Net.Http.ConnectHelper.ConnectAsync(Func
回调, DnsEndPoint 端点, HttpRequestMessage requestMessage, CancellationToken 取消令牌) IOException:IDX20804:无法从以下位置检索文档: '系统.字符串'。 Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(字符串 地址,CancellationToken 取消)
InvalidOperationException:IDX20803:无法获取配置 来自:'System.String'。 Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken 取消)
注意:我可以正确访问IdentityServer的主页,直接通过url及其端口访问。
我确定客户端配置正确,因为之前使用容器授权工作正常。
我该如何解决这个问题?
非常感谢
【问题讨论】:
-
仅供参考,
EXPOSE指令仅用于文档,除非使用-P标志运行。当像您这样指定端口时,它毫无意义,并且不提供任何有价值的信息 -
您好 Pinkfloydx33,感谢您的评论。端口已正确绑定,因为我可以通过浏览器访问 IdentityServer。现在,我将编辑问题。
-
这不是一个答案,只是一个信息性评论,让您知道您不需要向 我们 甚至向 docker 提供该信息
-
“问题是当我尝试启动应用程序时” - 你究竟在哪里启动哪个应用程序?听起来好像您尝试在主机系统上启动它(在 docker 之外,例如 Visual Studio)。 docker 主机名之外的“identityserver”不可用。
-
“我确定客户端配置正确”我会仔细检查。内部主机名无法与其他配置一起使用。在 oidc 流程中,您的浏览器应该如何重定向到“identityserver”?
标签: c# docker docker-compose yaml blazor-server-side