Solving Cross Domain issues on WCF

Problem

You want to access your services from other domain, especially using ASP.NET AJAX. However, the WCF doesn’t allow you to do so because of security concern.

Solution

The solution should be adressed by configuring both WCF service and also the client.

WCF Service Configuration

The idea is the WCF should add the header that permits the request from others domains. In order to do so, the WCF should be configured by adding several information on web.config and some codes.

  • Add two files CustomHeaderMessageInspector.cs and EnableCrossOriginResourceSharingBehavior.cs. These two files will help the WCF additional information the header
  • Replace the namespace with the name of project on the web,config. Please see the web.config template below.
  • Add the endpointBehaviors with <crossOriginResourceSharingBehavior/>.

Client Configuration

If you are using AJAX to do Cross Origin Resources Sharing (CORS), you should add the codes before the code is executed. For example

$.support.cors = true;

$.ajax({

            url: "urlwcf.com",

            type: "GET",

            dataType: "json",

beforeSend: function (xhr) {

xhr.setRequestHeader('Content-Type', 'text/plain');

},

            success: function (resultData) {

                ......

           }

        });

References

Please download the cross domain template belows. Thank you for @theo who helpd to prepare the template codes. Some good books also recommended for you

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Topics Highlights

About @ridife

This blog will be dedicated to integrate a knowledge between academic and industry need in the Software Engineering, DevOps, Cloud Computing and Microsoft 365 platform. Enjoy this blog and let's get in touch in any social media.

Xbox

Month List

Visitor