无法建立连接,因为目标机器主动拒绝它 127.0.0.1:port with subdomain

No connection could be made because the target machine actively refused it 127.0.0.1:port with subdomain

我正在 运行 安装一个 ASP.NET MVC 5 应用程序,它还托管 IdentityServer3。正如许多其他人之前所经历的那样,当我连接到此端点时...

http://tenant1.localhost:51515/identity/.well-known/openid-configuration?client_id=backoffice&redirect_uri=http%3A%2F%2Flocalhost%3A37046%2Findex.html&response_type=id_token%20token&scope=openid%20all_claims&state=1793477650&nonce=1172967295

...我收到以下错误:

No connection could be made because the target machine actively refused it 127.0.0.1:51515 at System.Net.HttpWebRequest.GetResponse() at RestSharp.Http.GetRawResponse(HttpWebRequest request) at RestSharp.Http.GetResponse(HttpWebRequest request)

(注意:在你确定这是重复之前,请阅读到问题的最后 - 我在来这里寻求帮助之前已经完成了功课)

当我使用 HttpWebRequestcURL 甚至 Go 中的应用程序时,结果相同。以下是我使用的一些代码示例:

C#(使用 RestSharp):

var client = new RestClient("http://tenant1.localhost:51515/identity/.well-known/openid-configuration?client_id=backoffice&redirect_uri=http%3A%2F%2Flocalhost%3A37046%2Findex.html&response_type=id_token%20token&scope=openid%20all_claims&state=1793477650&nonce=1172967295");
var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", "a09c64d2-e0c6-a416-d5ad-92079f0676b9");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);

卷曲:

curl -X GET \
'http://tenant1.localhost:51515/identity/.well-known/openid-configuration?client_id=backoffice&redirect_uri=http%3A%2F%2Flocalhost%3A37046%2Findex.html&response_type=id_token%20token&scope=openid%20all_claims&state=1793477650&nonce=1172967295' \
-H 'cache-control: no-cache' \
-H 'postman-token: 271bbbe6-bcb1-b999-80e5-9193f0c134ba'

我为这些示例做了一些替代,例如通过包含主机 header(值 tenant1.localhost:51515)或使用相同的 uri 作为 Web 客户端的代理。不幸的是他们都return同样的错误。

很奇怪 所有 我用浏览器或 Postman 发出的请求都成功了。 JavaScript 连接到端点的代码也有效。有一个 例外:一旦我有了 Fiddler session 运行ning,这是我从 Postman 得到的回复:

[Fiddler] The connection to 'tenant1.localhost' failed. <br />Error: ConnectionRefused (0x274d). <br />System.Net.Sockets.SocketException No connection could be made because the target machine actively refused it 127.0.0.1:51515

几天来我一直在寻找解决方案,但我似乎找不到合适的解决方案,但似乎很明显,只有在服务器端代码中才会出现问题。到目前为止,这是我尝试过的。我包含了 applicationhost.config 和主机文件来展示我如何为我的 Web 应用程序创建 'enabled' 子域(在内部用于识别租户)。此外,我将 IIS Express 用于本地开发,将 IIS 用于生产环境。

Applicationhost.config

<site name="MyApp" id="5">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="D:\Source\MyApp" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:51515:localhost" />
        <binding protocol="http" bindingInformation="*:51515:tenant1.localhost" />
    </bindings>
</site> 

主机文件

127.0.0.1 tenant.localhost

Netstat

这是我执行netstat -anb:

时的结果

这是netstat -na | find '51515'的结果:

我不确定这些值是什么意思,所以我可以在这里使用一些输入。 Just to make 确实我断开了与 Internet 的连接并禁用了防火墙和防病毒扫描程序,但没有结果。

代理

这些是我的 Internet Options 设置。如您所见,一切都已签出:

我尝试了各种与 web/app.config 文件中的代理设置的组合。我认为这不会在解决问题方面发挥重要作用,因为我的 Golang 应用程序也有同样的问题(这只是 Postman 生成的代码片段)。我什至尝试通过将 url 设置为 http://127.0.0.1:8888 来使用 Fiddler 作为代理。不出所料,WebRequest 实例的任何服务器端代理也无济于事。

<system.net>
  <defaultProxy> 
    <proxy usesystemdefault="False"/> 
  </defaultProxy>
</system.net>

Visual Studio

问题

鉴于关于这个主题的众多问题,我看到的唯一显着区别是我在 url 中使用了子域。每当我不使用子域时,一切都完美无缺!

如果这个假设似乎是正确的,我怎样才能欺骗 DNS、防火墙或任何其他阻止机制来接受来自子域的请求?也许代理可以帮助?

netstat 输出显示仅使用了 IP v6 地址,这不是很典型,但如果由于某种原因机器上未使用 IP v4 应该没问题。那么你不能指望服务器处理 IPv4 数据包(到 127.0.0.1)。

一个快速的解决方案是在 hosts 文件中设置一条记录 [::1] 而不是 127.0.0.1.

虽然Lex Li的回答更好,但我只是想提供一个替代方案来解决这个问题。我将我的 C# 代码示例从问题扩展为:

var client = new RestClient("http://tenant1.localhost:51515/identity/.well-known/openid-configuration?client_id=backoffice&redirect_uri=http%3A%2F%2Flocalhost%3A37046%2Findex.html&response_type=id_token%20token&scope=openid%20all_claims&state=1793477650&nonce=1172967295");
client.Proxy = new WebProxy("http://localhost:51515");

var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", "a09c64d2-e0c6-a416-d5ad-92079f0676b9");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);

所以回答我自己的问题:是的,代理可以提供帮助。通过使用不包含子域的 URI 向 RestClient 添加代理,webrequest 就像一个魅力。