Enable Application Mode in Dynamics 365 and Retail Server
Tags: dynamics, retailserver, microsoft
Published: 2018-10-11 22:23:10

---
title: Enable Application Mode in Dynamics 365 and Retail Server
published: true
description: Retail Server supports Service to Service authentication which makes it possible to have services capable of communicating with Retail Server without a need of a user in front of a screen to provide credentials at some point in time.
tags: dynamics, retailserver, microsoft
---

The Retail Server is a wrapper over commerce runtime. Retail Server uses a web API and OData to support thin clients both in the store and online on tablets and phones. The commerce runtime communicates with Retail Headquarters through Commerce Data Exchange services.

Retail Server supports Service to Service authentication which makes it possible to have services capable of communicating with Retail Server without a need of a user in front of a screen to provide credentials at some point in time. This enables scenarios when you need to create processes to periodically contact Retail Server to perform different kind of tasks, for instance, synchronizing customers, orders, publishing products for eCommerce functionality and so on.

To setup the feature the following major steps should be done:

1. Create an application in Azure Active Directory, this application will correspond to your Daemon application - the one which will make calls to Retail Server.
2. Add the application’s details into RS’s authentication white list, this way RS will be able to accept requests coming from that application.

These are described in detail below.

#### Create Application in Azure Active Directory

![image 1](https://thepracticaldev.s3.amazonaws.com/i/3txyr9medtkbf7thwmd0.png)

Navigate to [Portal](https://manage.windowsazure.com) and select Azure Active Directory

Select App Registrations and Create New Application Registration.

1. Enter Name and Url.
2. Select Type as Web App / Api.
3. Create button

![Image 2](https://thepracticaldev.s3.amazonaws.com/i/qq0cx46vtgscc61npqyt.png)
![image 3](https://thepracticaldev.s3.amazonaws.com/i/pverzacggnznaavcmmmk.png)

Open the Application created, Copy the application ID and Click Keys.

![image 4](https://thepracticaldev.s3.amazonaws.com/i/2esu8n09v59j27jrq6qc.png)

Create New Key ; Make sure you copy the key, as it will show the Key Only Once and keep it hidden and the only way is to get new key (if lost).

![image 5](https://thepracticaldev.s3.amazonaws.com/i/06ul60nzbkj2e0vryk4u.png)

Now you have both Application/ Client ID and Client Secret Key which will be further used in application and D365.

#### Add Application Detail into Retail Server White List

Login to Dynamics 365 ; Open Retail Shared Parameters in D365 from (Retails and Commerce > Headquarters Setup > Parameters)

By default, tenant is already present in “Identity Providers”, if not, you need to add your organization tenant.

Select your tenant and Click Add on “Relying Parties”.

1. Enter Client ID
2. Select Type as confidential
3. Select UserType as Application

Add Retail Server URL in “Server Resource IDS”

![image 5](https://thepracticaldev.s3.amazonaws.com/i/elicyb6x3wtqwtsc92uy.png)

Go to - Retail and Commerce > Retail IT > Distribution Schedule

Run Job 1110 (Global Configuration). Wait until the job finishes its work, then, in addition, if you don’t want to wait until cache expires, and if that is not production environment - you can execute iisreset.

![image 6](https://thepracticaldev.s3.amazonaws.com/i/hijbojny15l4fm0s3p4s.png)

#### Connect from Code

You need to install nuget package “Microsoft.IdentityModel.Clients.ActiveDirectory” to connect

These are the few terms i would be using to create retail server authenticated context.

1. D365Url : This is the retail server URL of the D365 Example: https://testServerret.cloud.onebox.dynamics.com/
2. retailServerUri : This is the retail Server metadata URL Example: https://testServerret.cloudax.dynamics.com/Commerce/
3. Client_Id : This is the client Id of the application created in azure portal
4. Client_Secret :This is the client secret key of the application created in azure portal
5. Azure_Active_Directory : This is the tenant of the D365. Format: https://sts.windows.net/TENANT_ID ; Example: https://sts.windows.net/7yu653w-65d3-4u96-a135-8ui4ec1672e3/
6. OperatingUnitNumber : Online Store Number

```
AuthenticationContext authenticationContext = new AuthenticationContext(azureActiveDirectory, false);
AuthenticationResult authResult = null;
authResult = await authenticationContext.AcquireTokenAsync(D365Url, new ClientCredential(clientId, clientSecret));
ClientCredentialsToken clientCredentialsToken = new ClientCredentialsToken(authResult.AccessToken);
RetailServerContext retailServerContext = RetailServerContext.Create(this.retailServerUri, this.operatingUnitNumber, clientCredentialsToken);
return ManagerFactory.Create(retailServerContext);
```