Authenticated WCF service that returns JSONP

Posted: November 11, 2011 in .NET
Tags: , ,

Today I was challenged with creating an authenticated, cross-domain, WCF service that returns JSONP.

 Using the ‘out of the box’ WCF configurations this is not possible.  If you attempt to use authentication with an endpoint that allows cross-site scripting, you will recieve an error message like this –

Cross domain javascript callback is not supported in authenticated services

To get around this restriction (It’s not a bug) we can use a custom encoder factory/binding/etc…

In my application, I am using the sample JSONP library that was provided by Miscrosoft.
http://msdn.microsoft.com/en-us/library/cc716898(v=VS.90).aspx

Then added the appropriate authentication requirement in the web config.

<customBinding>
<binding name=”jsonpBinding”>
<jsonpMessageEncoding/>
<httpTransport manualAddressing=”true” authenticationScheme=”Negotiate” />
</binding>
</customBinding>

 

If anyone needs help with this, First try to implement the JSONP library from Microsoft and let me know if you get stuck. I’ll try to help you configure your app.

http://msdn.microsoft.com/en-us/library/cc716898(v=VS.90).aspx

Advertisements
Comments
  1. Cory says:

    I would be very interested in seeing the code/markup for this. I’m fairly green when it comes to WCF, and this would really help me fix my current issue

    Thanks much,
    Cory

  2. Ringo says:

    Hello,

    Can you send me also your solution/code about the authentication of the WCF webservice with JSONP.
    I would be very glad.

    Thanks
    Ringo

  3. Cesar Leyva says:

    Really nice, it worked like charm!! 🙂

  4. Ali says:

    Hi Jeremy,
    Can you send me the code for the solution please? I am having problem to get jsonp data form a WCF service with basic authentication. I have spent long time searching for a solution.

  5. Ali says:

    Hi
    Iam having trouble implementing Restful WCF api with Basic authentication that can be called using AJAX. Can you send me the code please Jeremy? I would be very thankful. I am waiting you email.

    Br.

  6. Guru says:

    Hi,

    I tried to use this custom binding with Basic authentication but it doesn’t seems to work. I am getting Access is Denied error.

    Service side change

    httpTransport manualAddressing=”true” authenticationScheme=”Basic”

    When I call from the client, I am using xmlhttprequest and I set the Authorization request header using Base64encoded username and password but still I am getting this error.

    Let me know if am doing something wrong.

    Thanks
    Guru

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s