Freigeben über


Use OAuth to Authenticate with the CRM Web Services

Hey guys,

Today I'm going to talk about an interesting sample on Windows store apps which will go connect with CRM using Oauth.

You'll need a working knowledge on the following

  1. ADAL (Active Directory Authentication Library) - Available on Nuget
  2. ADFS 3.0
  3. Window Store Apps
  4. A bit of Fiddler (Optional)

I'm trying to walk you through the entire steps which includes the configuration of ADFS, then we'll go ahead and build the sample.

Register your application with ADFS Environment for development / Token issue

  1. Create a GUID using Guid Generator using Visual Studio
  2. Make a note of it for registration
  3. At this point, I've not provided Redirect Url

Adfs script:

Add-AdfsClient -ClientId C8E3F34A-E1A6-455C-A6DB-A1B82E6A6BE0 -Name WindowsAppsStore -Description "OAuth for CRM connecting Clients from Windows Store"

Windows Store Application demo

Note: This app is using Mobile SDK Please read the instructions to compile and generate the Assembly for app reference

  1. Create a new project using Visual Studio 2013
  2. Choose, Project for Store (Blank should be okay)
  3. Add Reference to "Microsoft.Crm.Sdk.Mobile"
  4. Add CrmHelper.cs file form the Mobile SDK Download to reuse the code.
  5. I have written and attached below

Few Global Variables

ADAL Based Sample:

It's important that you run the code for the first time with all the correct parameters/variables set and obtain the redirect URL from below code:
public static string redirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();

Once you've see the value, it will look something like this
ms-app://s-1-15-2-23142
Now go back to ADFS and use the below command:

Set-AdfsClient -ClientId C8E3F34A-E1A6-455C-A6DB-A1B82E6A6BE0 -Name WindowsAppsStore -Description "OAuth for CRM connecting Clients from Windows Store" -RedirectUri ms-app://s-1-15-2-23142

Once you execute and deploy your application. You can see the token value in fiddler as below:

         HTTP/1.1 200 OK
        Cache-Control: no-store
        Pragma: no-cache
        Content-Length: 1115
        Content-Type: application/json;charset=UTF-8
        Server: Microsoft-HTTPAPI/2.0
        client-request-id: 43224044-4a95-41cc-9716-fe9387529c49
        Date: Tue, 06 Jan 2015 18:52:44 GMT
        {"access_token":"token value encrypted>":491520}

My special credits to Kenichiro Nakamura for inspiring and providing technical insights to use Mobile SDK for CRM.

Cheers,
Apurv

MainPage.xaml.zip