在同一IIS服务器上,从站点A 连接到站点B 时出现问题。
站点A 充当SPA(aspx),并且在.net 4.6.1上都使用站点B 的API(网络api)。
在我的部署环境中,它们都是位于同一V服务器上的同一域的子域。
如果我在开发环境中,则可以使用以下代码:
protected async Task<HttpResponseMessage> Get(string urlParam = "")
{
string url = _url;
if (!string.IsNullOrWhiteSpace(urlParam))
{
url = $"{_url}?{urlParam}";
}
NetworkCredential credentials = null;
if (!string.IsNullOrEmpty(ServiceUser))
{
credentials = !string.IsNullOrEmpty(ServiceuserDomain)
? new NetworkCredential(ServiceUser, Servicepassword, ServiceuserDomain)
: new NetworkCredential(ServiceUser, Servicepassword);
}
using (var handler = new HttpClientHandler {Credentials = credentials})
{
using (var httpClient = new HttpClient(handler))
{
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.GetAsync(url);
}
...
我还可以将该代码与已部署的API(站点B )
连接站点A 的web.config没有“特殊”配置。启用了匿名身份验证,并禁用了Windows身份验证。
站点B 的web.config具有以下“特殊”配置:
<system.web>
<compilation debug="true" targetFramework="4.6.1" defaultLanguage="c#" />
<httpRuntime targetFramework="4.6.1" />
<customErrors mode="Off" />
<authentication mode="Windows"/>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
<location path="swagger">
<system.web>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="api">
<system.web>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
除了/ api和/ swagger以外,基本上允许匿名访问该网站。
如果我使用新的BETA身份验证NTLM测试了从POSTMAN对 SITE B 的API调用,它也可以正常工作。凭据绝对没有错,但是每当我通过 SITE B 的日志文件时,我都会看到:
2019-06-03 12:08:43 212.147.60.15 GET /api/form/ from=0&to=99/ 443 - IP HTTP/1.1 - - sub.domain.ch 401 0 0 2661 114 0
2019-06-03 12:08:43 212.147.60.15 GET /api/form/ from=0&to=99/ 443 - IP HTTP/1.1 - - sub.domain.ch 401 1 3221225581 6661 773 0
此外,该服务器上的两个子域都具有相同的IP地址,但我认为这无关。 不幸的是,我无法访问Windows Server。我只有 PLESK 访问权限来管理域/文件。