页面上的服务调用生成区域安全错误
Service Call on Page Generates Zone Security Error
此错误是在 ajax 调用
时收到的
WebSocket Error: SECURITY_ERR, Cross zone connection not allowed
也返回了 500 错误代码。 经过进一步测试,我能够得到似乎与错误无关的其他响应。请参阅下面的报告错误所在。
来自这个 Angular 调用
$http.post("Status.aspx/GetDataAsync", {})
.then(function(response){ $scope.theData = data;},
function(response){ $scope.result = "Error!";}
);
当试图调用 WebMethod
后面的页面代码时,这很明显 也会调用 其余 Web 服务的网络服务。
[System.Web.Services.WebMethod]
public static async Task<string> GetDataAsync()
{
var httpresult = await (new HttpClient()).GetAsync("{Internal site rest service Url}");
return await httpresult.Content.ReadAsStringAsync();
}
这让我想起了一个旧的 Silverlight cross-domain 问题电话……但我离题了。
问题
区域问题可以解决还是必须直接呼叫休息服务?
尝试CORS的使用(见Enabling Cross-Origin Requests (CORS))在方法层面如
[System.Web.Services.WebMethod]
[EnableCors("AllowAllOrigins", "AllowHeaders", "AllowAllMethods")]
运气不好。
错误
这是在Edge(和IE)上的F12工具中发现错误的地方。 Chrome 没有报告问题。
您需要设置页面的内容安全策略 (CSP),以便浏览器不会阻止您的{Internal site rest service Url}。
以下是帮助我解决此问题的链接:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src
http://caniuse.com/#feat=contentsecuritypolicy
对于 MVC,我在 web.config 中放置了以下内容:
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="connect-src 'self' wss://localhost:7717" />
</customHeaders>
</httpProtocol>
然后我能够创建到 wss://localhost:7717 的 websocket 连接,但无法连接到任何其他端口。
浏览器阻止此类请求以防止 cross-zone 脚本攻击,因此您需要通过 http header 完成页面设置策略。我正在研究类似的问题。我错过了一些东西,但我认为这是正确的方向。我需要其他人的意见,因为这并不能完全解决问题。不过更近了。
此错误是在 ajax 调用
时收到的WebSocket Error: SECURITY_ERR, Cross zone connection not allowed
也返回了 500 错误代码。 经过进一步测试,我能够得到似乎与错误无关的其他响应。请参阅下面的报告错误所在。
来自这个 Angular 调用
$http.post("Status.aspx/GetDataAsync", {})
.then(function(response){ $scope.theData = data;},
function(response){ $scope.result = "Error!";}
);
当试图调用 WebMethod
后面的页面代码时,这很明显 也会调用 其余 Web 服务的网络服务。
[System.Web.Services.WebMethod]
public static async Task<string> GetDataAsync()
{
var httpresult = await (new HttpClient()).GetAsync("{Internal site rest service Url}");
return await httpresult.Content.ReadAsStringAsync();
}
这让我想起了一个旧的 Silverlight cross-domain 问题电话……但我离题了。
问题 区域问题可以解决还是必须直接呼叫休息服务?
尝试CORS的使用(见Enabling Cross-Origin Requests (CORS))在方法层面如
[System.Web.Services.WebMethod]
[EnableCors("AllowAllOrigins", "AllowHeaders", "AllowAllMethods")]
运气不好。
错误
这是在Edge(和IE)上的F12工具中发现错误的地方。 Chrome 没有报告问题。
您需要设置页面的内容安全策略 (CSP),以便浏览器不会阻止您的{Internal site rest service Url}。
以下是帮助我解决此问题的链接: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src http://caniuse.com/#feat=contentsecuritypolicy
对于 MVC,我在 web.config 中放置了以下内容:
<httpProtocol> <customHeaders> <add name="Content-Security-Policy" value="connect-src 'self' wss://localhost:7717" /> </customHeaders> </httpProtocol>
然后我能够创建到 wss://localhost:7717 的 websocket 连接,但无法连接到任何其他端口。
浏览器阻止此类请求以防止 cross-zone 脚本攻击,因此您需要通过 http header 完成页面设置策略。我正在研究类似的问题。我错过了一些东西,但我认为这是正确的方向。我需要其他人的意见,因为这并不能完全解决问题。不过更近了。