【发布时间】:2021-01-18 03:32:06
【问题描述】:
我目前正在使用 IdentityServer4 开发 .NET Core 3.1 应用程序。
我的应用程序如下所示:
- .NET Core 3.1 IdentityServer4 (Windows Server 1 - VM)
- .NET Core 3.1 WebApi (Windows Server 2 - VM)
两个应用程序都在 IIS 上使用有效的 HTTPS 通配符证书 - TLS/SSL 运行。
我使用授权码 + PKCE 流程。
当我在 localhost 上运行我的应用程序时,它就像一个魅力,但是当我在部署的 windows 服务器上运行它们时,似乎 Api 找不到 IdentityServer4 发现文档并且我收到此错误:
IDX20803:无法从以下位置获取配置:'https://myServer/env/.well-known/openid-configuration
完整的错误如下所示:
"message": "连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立 连接失败,因为连接的主机没有响应。", "type": "System.Net.Sockets.SocketException", “raw”:“System.Net.Sockets.SocketException(10060):连接尝试失败,因为连接方没有正确响应 一段时间后,或建立连接失败,因为 连接的主机未能响应。\r\n at System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口, CancellationToken 取消令牌)", “堆栈帧”:[ { “功能”:“System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,int端口, CancellationToken 取消令牌)" } ]
在我的浏览器中,当我调用 url 时,我可以看到 .well-known 引用:https://myServer/env/.well-known/openid-configuration。
here已经发布了一个类似的问题。
在我的 WebAPI 项目中,我需要使用 JWTBearer 和 Cookie 模式。 WebAPI 连接选项如下所示:
services.AddAuthentication(
options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddJwtBearer(
options =>
{
options.RequireHttpsMetadata = true;
string authority = "https://myAuth.com/env";
options.Authority = authority;
options.Audience = "MyApi1";
options.MetadataAddress = $"{authority}/.well-known/openid-configuration";
options.TokenValidationParameters = new TokenValidationParameters
{
RequireAudience = true,
RequireExpirationTime = true,
ValidateAudience = true,
ValidateActor = true,
ValidateLifetime = true,
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
RoleClaimType = "role",
NameClaimType = "name"
};
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(
OpenIdConnectDefaults.AuthenticationScheme,
options =>
{
string authority = "https://myAuth.com/env";
options.Authority = authority;
options.MetadataAddress = $"{authority}/.well-known/openid-configuration";
options.ClientId = "Swagger_Docu";
options.ClientSecret = "secret";
options.SaveTokens = true;
options.ResponseType = "code";
options.UseTokenLifetime = true;
options.RequireHttpsMetadata = true;
options.Scope.Add("offline_access");
});
您知道如何在我的 .NET Core 3.1 API 中从 IdentityServer4 正确获取 .well-known/openid-configuration 吗?
【问题讨论】:
-
错误信息只是一个标准的超时信息。您可能在 HTTP 请求之前发生的 TLS 身份验证失败。我会使用嗅探器并验证 TLS 版本,以查看您是否使用 TLS 1.2/1.3(不是 1.0/1.1)以及是否收到 HTTP 响应。检查状态代码 200 OK(或 400/500 错误)的响应。最后一次使用其他应用程序是什么时候?它仍然有效吗? 6 月,微软做了一个安全更新,在服务器上禁用了 TLS 1.0/1.1,现在客户端必须使用 TLS 1.2/1.3。
-
最好的调试是使用像wireshark或fiddler这样的嗅探器。比较工作与不工作。您没有专门在 c# 中指定 TLS 版本。所以默认的 TLS 版本取决于 Net 的版本和 windows 的版本。 IIS 是一个旧的操作系统,可能默认为 TLS 1.0,这将无法正常工作。您必须修改 c# 代码以添加 TLS 版本。
-
我使用的是 TLS/SSL 1.2 证书,并且连接有效且加密。
-
是的。加密模式可以不同(参见 wiki:en.wikipedia.org/wiki/Transport_Layer_Security),或者服务器可以配置为接受一种而不接受另一种。通常,Microsoft 每次发布新网络时都会将默认值更新为最新和最好的,因此用户旧代码会中断。
-
这是 HTTPS 问题,正如其他评论所提到的。也检查您的 IP,这里有一个示例问题,例如您的 github.com/IdentityServer/IdentityServer4/issues/… 。另外你不需要设置
MetadataAddress,它的值只在没有设置权限的情况下使用,参考:github.com/dotnet/aspnetcore/blob/master/src/Security/… & github.com/dotnet/aspnetcore/blob/master/src/Security/…
标签: c# api .net-core https identityserver4