{"id":3448,"date":"2018-10-23T06:25:35","date_gmt":"2018-10-23T06:25:35","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3448"},"modified":"2018-11-13T19:54:57","modified_gmt":"2018-11-13T19:54:57","slug":"dynamics-365-ce-and-azure-function-part-3","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3448","title":{"rendered":"Dynamics 365 CE and Azure Function Part 3"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nThis is our third article in the Dynamics 365 and Azure series, in first article we discussed basic introduction about azure function and set azure function app. In second article we discussed how we can connect to Dynamics 365 organization using hard coded user name and password. Today we are going to discuss how we can get rid of hard coded credentials and use server to server authentication.<\/p>\n<p><strong>Details<\/strong><br \/>\nTo implement server to server authentication we need three following things<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3452 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser4-300x85.png\" alt=\"azureuser4\" width=\"300\" height=\"85\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser4-300x85.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser4.png 381w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nFirst we need to get tenant id so login to Azure portal and navigate to <strong>Azure Active Directory -&gt; Properties and note down Directory ID<\/strong><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser15.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3464 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser15-300x135.png\" alt=\"azureuser15\" width=\"300\" height=\"135\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser15-300x135.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser15-624x282.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser15.png 749w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nOnce we got our tenant id, now we need to get application id and key details. We need application id to setup application user in Dynamics 365. To get these details we need to register app in <strong>Azure Active Directory<\/strong>, so now navigate to Azure Active Directory and click on <strong>New application registration<\/strong><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3449 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser1-300x148.png\" alt=\"azureuser1\" width=\"300\" height=\"148\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser1-300x148.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser1-624x308.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser1.png 723w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Provide your app details and click on <strong>Create<\/strong><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3450 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser2-286x300.png\" alt=\"azureuser2\" width=\"286\" height=\"300\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser2-286x300.png 286w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser2.png 321w\" sizes=\"(max-width: 286px) 100vw, 286px\" \/><\/a><\/p>\n<p>Once application registration is created and note down <strong>Application ID<\/strong><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser31.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3457 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser31-300x42.png\" alt=\"azureuser3\" width=\"300\" height=\"42\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser31-300x42.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser31.png 309w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nGo to Dynamics 365 organization and navigate to <strong>Settings-&gt;Security-&gt; Users<\/strong> and change view to <strong>Application Users<\/strong>. Click on New button to add application user, <strong>Make sure Application User form is selected<\/strong>, use application id we copied from the Azure portal, provide email address and create application user. Once user is created add required security role for this user.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3459 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser7-300x190.png\" alt=\"azureuser7\" width=\"300\" height=\"190\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser7-300x190.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser7-624x396.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser7.png 640w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nNext we need never expire key for our application. Click on <strong>Settings -&gt; Keys<\/strong> and add a nearexpire key<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3458 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser6-300x34.png\" alt=\"azureuser6\" width=\"300\" height=\"34\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser6-300x34.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/azureuser6.png 621w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nNow we have all required information ready, we can use these details in our following code in our Azure function trigger.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n private static OrganizationWebProxyClient GetCRMService() {\r\n\r\n  var aadInstance = &quot;https:\/\/login.microsoftonline.com\/&quot;;\r\n  var organizationUrl = &quot;https:\/\/yourDynamicsCRM.crm.dynamics.com&quot;; \r\n\r\n  var tenantId = &quot;aeb....&quot;;\r\n\r\n  var applicationId = &quot;c86...&quot;;\r\n\r\n  var key = &quot;7jNY.....&quot;;\r\n\r\n  var clientcred = new ClientCredential(applicationId, key);\r\n\r\n  var authenticationContext = new AuthenticationContext(aadInstance + tenantId);\r\n\r\n  var authenticationResult = authenticationContext.AcquireTokenAsync(organizationUrl, clientcred);\r\n\r\n  var requestedToken = authenticationResult.Result.AccessToken;\r\n\r\n  var sdkService = new OrganizationWebProxyClient(new Uri(organizationUrl +\r\n\t\t\t@&quot;\/xrmservices\/2011\/organization.svc\/web?SdkClientVersion=8.2&quot;), false);\r\n  sdkService.HeaderToken = requestedToken;\r\n\r\n  return sdkService;\r\n }\r\n<\/pre>\n<p>Now we can update our code to use OrganizationWebProxyClient method like following.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nOrganizationWebProxyClient service = GetCRMService();\r\n<\/pre>\n<p>We also need to add following reference to use OrganizationWebProxyClient in our Azure function trigger.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.Xrm.Sdk.WebServiceClient;\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/?p=3371\">Dynamics 365 CE and Azure Function Part 1<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3425\">Dynamics 365 CE and Azure Function Part 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This is our third article in the Dynamics 365 and Azure series, in first article we discussed basic introduction about azure function and set azure function app. In second article we discussed how we can connect to Dynamics 365 organization using hard coded user name and password. Today we are going to discuss how we can get rid of&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=3448\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[491,21,402],"tags":[654,665,662,670,664],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3448"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3448"}],"version-history":[{"count":12,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3448\/revisions"}],"predecessor-version":[{"id":3471,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3448\/revisions\/3471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/3407"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}