简体   繁体   中英

Accessing WCF REST service through IIS using CORS works locally but not from connected device

I have a set of WCF services that I have enabled WebInvoke for. One way that I want access them is through javascript on a simple webpage hosted by IIS. In order to get this to work at all I had to enable CORS, which I managed using this guide: http://blogs.microsoft.co.il/idof/2011/07/02/cross-origin-resource-sharing-cors-and-wcf/

However there is still an issue when I access the webpage across my local network from a different computer.

To give an example:

Two computers on the network Comp1 (10.0.0.1) and Comp2 (10.0.0.2). Both IIS and WCF services are hosted by Comp1.

Using Firefox I can access the webpage from Comp1 using: http:// 10.0.0.1:12345

This works as long as CORS is enabled according to the link above. If I disable CORS I get an error message saying that CORS is disabled.

If I use Firefox from Comp2 to access 10.0.0.1:12345 I get the same CORS error message no matter if I have the CORS code enabled in the WCF-server. The error message I get here is the same one as the one I get on Comp1 if CORS is disabled.

Does anyone have any suggestions on what might be wrong?

Everything (except js and html) is in C# using Visual Studio 2013. Please tell me if I should include some code to get better help.

Make sure you have custom header enabled for CORS support in IIS. If not add below settings to the root of web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

More details for enabling cors on server side and client side .

PS - You can also checkout this new WCF rest behavior extension library that does support the CORS enabled behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM