web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calling Web Services from javascript with Secure Token and ADFS

(0) ShareShare
ReportReport
Posted on by

We have an on premise deployment of CRM and are using ADFS with Secure Token to authenticate users when they hit our web services from javascript. The web services are hosted on a separate server from CRM on a different subdomain.

After a lot of struggles with cross origin security policy, we are finally very close to getting this to work. Our last problem has to do with how the ADFS security policy handles requests to resources on our web server.

If you access the web service directly through the browser URL, it works fine. This is because the request is intercepted by ADFS, authenticated, and then sent what amounts to an HTML page with a form that automatically submits itself to the originally requested URL, and this form submission is what gives the browser session its security token (seen in cookies as FedAuth and FedAuth1).

However, this process fails when a web service is requested through javascript. In our case, to get around the browser's cross origin security policy, we are using JSONP, but even without JSONP, I believe we would have the same problem with XMLHttpRequest.

Instead of receiving pure javascript (JSONP) or SOAP (XMLHttpRequest), the response from the web service is the HTML web form that wants to post back to itself. Javascript that makes a JSONP call is expecting pure javascript in the result, and so fails -- and since it doesn't post back, it never gets the security token.

Some of our pages have iframes that point to web pages hosted on the same server as our web services. We have found that those pages successfully get their security token because the iframe requests the resource, gets the HTML with the form autopostback, performs the postback, gets the security token, and displays the results of the postback. At that point going forward, any web services using JSONP are successful because they now contain the security token.

My question is: how can I get my JSONP web service calls to retrieve the security token before making their call?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    ScottDurow Profile Picture
    21 on at

    Hi,

    I've developer lots of solutions that need external webservice calls as part of the user interface - and I've found that by far the best approach is to secure them using CRMs own authentication and authorisation. You register a plugin on a custom entity on the RetrieveMultiple step - the plugin can then check the UserId and then make the service call using WCF under a 'service account' identity. The results are then output as Entity records so that you can call it easily via JavaScript.

    This technique has the advantage that the client only needs to authenticate with CRM and all the external authentication is handled server side.

    You can find all the code you need to do this intercepting of RetrieveMultiple in my sample - code.msdn.microsoft.com/.../Discussions

    hth,

    Scott

  • Community Member Profile Picture
    on at

    That seems like an interesting approach but I'm not sure that it will work for us without more information. Is it possible to use your approach for web services that are called interactively on the form? We have a ton of web services that we call on the fly as data is changed on forms and which make extensive use of our business logic library.

  • ScottDurow Profile Picture
    21 on at

    Hi,

    Yes it works well when called interactively from the form. As far as the javascript is concerned it is querying CRM. It saves so much hassle when deploying as well and allows you to expose your external services via IFD deployments as well.

    hth,

    Scott

  • ScottDurow Profile Picture
    21 on at

    Also - with the new CRM2013 custom actions feature, you don't have to use a RetrieveMultiple, you can essentially create a custom SDK webservice message.

    Very cool!

  • Community Member Profile Picture
    on at

    I'm not connecting the dots between your solution and how I can use it to call web services from javascript.

    Let's say I have a javascript web resource on a custom entity, and I've wired it up so that during form load, an onchange event handler is registered for one of the fields. During this onchange event, I want to call an external web service. How would I do so using your solution?

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    The way you do this is to perform a fetchxml query from your javascript on the custom entity (using something like the XrmSvcToolkit) - this then invokes the plugin and you can extract the fetchxml criteria (which serve as web service parameters)  and then do your custom webservice call. The entities that are returned are the results from the custom service call that can then be used by the javascript.

    Does that makes sense? I keep meaning to do a blog post about this!

  • Community Member Profile Picture
    on at

    That does make sense. Silly me, I didn't see the Description tab on the link to your solution, that would have saved us both some time!

    I'm stuck between a rock and a hard place right now: we've got a lot of web service calls peppered everywhere, and to change how we're doing those calls will take some time (I went through that once already, changing them to JSONP to overcome the cross domain security policy). It's either overcome that inertia or bang my head against the authentication wall some more.

    I really only need for the user's browser to hit a dummy web page hosted on the secure token server before any of the web services get called, and I'd be done. My kingdom for two little cookies.

    Anyway, thanks for the answers. I do like the solution and I'll mark this as answered.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans