IdentityServer4:无法从以下位置获取配置:'https://myServer/。well-known/openid-configuration

IdentityServer4: Unable to obtain configuration from: 'https://myServer/.well-known/openid-configuration

我目前正在使用 IdentityServer4 开发 .NET Core 3.1 应用程序。

我的申请是这样的:

IIS 上的两个应用程序 运行 都具有适用于 HTTPS 的有效通配符证书 - TLS/SSL。

我使用授权码+PKCE流程

当我 运行 我的应用程序在本地主机上工作时它很有魅力,但是当我 运行 它们部署在 windows 服务器上时,似乎 Api 找不到IdentityServer4 发现文档,我收到此错误:

IDX20803:无法从以下位置获取配置:'https:///myServer/env/.well-known/openid-configuration

完整的错误如下所示:

"message": "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.", "type": "System.Net.Sockets.SocketException", "raw": "System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\r\n at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)", "stackFrames": [ { "function": "System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)" } ]

在我的浏览器中,当我调用 url 时,我可以看到 .well-known 参考:https://myServer/env/.well-known/openid-configuration.

已发布类似问题

在我的 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 吗?

一个好的做法是对您的 IdentiyServer 执行一个简单的 HttpClient“您在吗,测试请求”,以确保您可以访问它。或者,如果您愿意,也可以让客户端等待并在放弃之前重试此 ping 几分钟。如果您有一些连接问题,那么您可以在启动时更容易地在日志中看到。或许同时执行 HTTP 和 HTTPS 请求以确保两者都能正常工作。-

您可以在 IdentityServer 中让您的主页 http://youridentityserver/ 请求 return 一些静态文本,例如

(在您的 HomeController 中)

public IActionResult Index()
{
    if (_environment.IsDevelopment())
    {
        // only show in development
        return View();
    }

    _logger.LogInformation("Homepage is disabled in production. Returning 404.");
    return NotFound("Service OK");
}

然后在您的 web 应用程序启动之前在您的客户端中查找“服务正常”。