Using CRM from an external website – part 9: Custom Authentication Service – Proof of concept

In this series, I’m investiging alternate ways to make a connection from an external website to the back-end Dynamics CRM system. In the previous article in this series, I posted some code snippets of the proof of concept I was working on.
In this article, I’ve written a proof of concept that actually connects from your external website to Dynamics CRM using Knockout.js and a custom Authentication Service.

The custom authentication service, takes the call you want to make to CRM, puts authentication to the call and forwards it to Dynamics CRM. The results are passed as is in JSON format back to the calling website.

Love-It

How does it work?

My goal was to make a proof of concept. This code is not yet ready for production.

As soon as the web project is launched, a web page appears with a Select link in it. When you click the link, the screen is populated with data stored in CRM.

Ko-result

As soon as I click the select link, the knockout viewmodel triggers a REST call to the Authentication Service. In that REST call, the actual CRM webservice call is passed as argument. For that particular reason I implemented the call to the Authentication service as an Http POST request.

Once the call is received by the Authentication Service, the posted data is captured. The posted data is forwarded to CRM using the C# WebClient object. The WebClient object gets the user credentials of the user that is allowed to make connection to CRM (see code below).
In order to receive the results as a JSON string, the WebClient is enriched with additional headers. To get the data from CRM (Http GET) we have to use the DownloadString(query) method. Once the JSON data is received, it is converted to a byte array which is streamed back to the calling website.

Ws_magic

What needs to be done?

As I mentioned before, the code I provide is a proof of concept. For that reason I implemented a GET method to get data from CRM. If you want to be able as well to POST data to CRM you’ll have to add a POST method (which is from a javascript perspective the same logic. The C# WebClient objects needs to call a different method to post the data.
I also implemented the Authentication Service as a console application. That has to be changed as well, e.g. write a Windows Service or a Web service to host the functionality.

Source code

For your convenience I put up the File Attachment: KnockOutCRM.zip (54 KB). This file contains the web project. Furthermore I put up the File Attachment: AuthenticationService.zip (6 KB). This file contains the authentication service.

In order to run the Authentication Service, you have to put valid credentials (user name, password, domain) in the program.cs file. The Authentication Service has to be run with elevated privilleges (run as administrator), otherwise the HttpListener will throw an Access Denied exception.

In the web project I hardcoded the URL of my development CRM server. You’ll have to change that URL. Furthermore I refer to an existing item in my Contacts entity by using a specific guid.

For now I’ve met my goal!
I’m able to connect to Dynamics CRM from an external website. If you want me to finish the Authentication Service, please let me know.

Thank you for reading.

6 thoughts on “Using CRM from an external website – part 9: Custom Authentication Service – Proof of concept

  1. […] Using CRM from an external website – part 9: Custom Authentication Service – Proof of concept […]

  2. Chris Harrington says:

    I don’t suppose you have found a way to pass in fixed username/password credentials? I am thinking about a use case where the site is completely JS calling the CRM site with credentials embedded in the code?

    • Hi Chris,

      As of CRM 2016 things have changed. Untill CRM 2015 you dont have the possibility to connect directly to CRM from within javascript.
      However there is a node.js sample out there by Jason Lattimer (but as far as I understand Node.js is totally different from normal javascript). The problem you will run into is created the authentication cookie. Once you tackled that hurdle, you will be able to use REST calls to access CRM.

      I hope this helps you further

      Bas

  3. Jamie says:

    Hey Bas,
    I am attempting to connect a Linux/Apache/Drupal external server with CRM 2016 On-Premise with ADFS.
    In one of your previous articles you mentioned that someone else you know was doing this as well. I would be interested to find out if they were successful. Your sample above seems quite complete but as you mention not production ready, in addition it appears to connect the the 2011 endpoint which Microsoft has deprecated with CRM 2016 update 1. I am looking at connecting to WebAPI endpoint only. If you can point me in any direction I would appreciate it!

  4. URL says:

    … [Trackback]

    […] Read More Infos here: journeyintocrm.com/archives/1209 […]

Leave a Reply

Your email address will not be published. Required fields are marked *