{"id":24610,"date":"2020-09-03T11:15:41","date_gmt":"2020-09-03T11:15:41","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=24610"},"modified":"2020-11-18T07:48:12","modified_gmt":"2020-11-18T07:48:12","slug":"impersonation-within-azure-function-or-custom-connector-when-using-aad-authentication","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2020\/09\/impersonation-within-azure-function-or-custom-connector-when-using-aad-authentication\/","title":{"rendered":"Impersonation within Azure Function or Custom Connector when using AAD authentication"},"content":{"rendered":"<p style=\"text-align: justify;\">In the earlier blog posts, we discussed <a href=\"https:\/\/www.inogic.com\/blog\/2020\/08\/building-custom-connectors-for-power-apps-and-power-automate-flows-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">setting up an Azure function with AAD authentication<\/a> and then creating a custom connector for the Azure function that also requires AAD authentication to make a <a href=\"https:\/\/www.inogic.com\/blog\/2020\/08\/building-custom-connectors-for-power-apps-and-power-automate-flows-part-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">connection to the connector<\/a>.<\/p>\n<p style=\"text-align: justify;\">Given that the Azure function is configured for AAD authentication in the Authentication \/ Authorization section of the function as shown below<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-24615 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1.png\" alt=\"Azure Function or Custom Connector when using AAD authentication\" width=\"1340\" height=\"692\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1.png 1340w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1-300x155.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1-768x397.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1-1024x529.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-1-660x341.png 660w\" sizes=\"(max-width: 1340px) 100vw, 1340px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Now that we have provided for AAD authentication that requires a user login, it would be good if all operations are executed within the context of the same user.<\/p>\n<p style=\"text-align: justify;\">Within your Azure function, you can get the details of the logged-in user using the <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-functions\/functions-bindings-http-webhook-trigger?tabs=csharp#working-with-client-identities\" target=\"_blank\" rel=\"noopener noreferrer\">ClaimsPrincipal<\/a><\/p>\n<p>ClaimsPrincipal principal = req.HttpContext.User;<\/p>\n<p>if (principal.Identity != null)<\/p>\n<p>{<\/p>\n<p style=\"padding-left: 40px;\">log.LogInformation(&#8220;Claims identity &#8221; + principal.Identity.Name);<\/p>\n<p>}<\/p>\n<p>if (principal.Claims != null)<\/p>\n<p>{<\/p>\n<p style=\"padding-left: 40px;\">foreach (Claim c in principal.Claims)<\/p>\n<p style=\"padding-left: 40px;\">{<\/p>\n<p style=\"padding-left: 80px;\">log.LogInformation(&#8220;CLAIM TYPE: &#8221; + c.Type + &#8220;; CLAIM VALUE: &#8221; + c.Value + &#8220;&lt;\/br&gt;&#8221;);<\/p>\n<p style=\"padding-left: 40px;\">}<\/p>\n<p>\u00a0}<\/p>\n<p>In the console, you can see all the claims returned<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-24613 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2.png\" alt=\"Azure Function or Custom Connector when using AAD authentication\" width=\"2438\" height=\"1852\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2.png 2438w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2-300x228.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2-768x583.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2-1024x778.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-2-660x501.png 660w\" sizes=\"(max-width: 2438px) 100vw, 2438px\" \/><\/a><\/p>\n<p>One of the claims returned is AADID<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-24614 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3.png\" alt=\"Azure Function or Custom Connector when using AAD authentication\" width=\"2438\" height=\"66\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3.png 2438w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3-300x8.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3-768x21.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3-1024x28.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/09\/Impersonation-within-Azure-Function-or-Custom-Connector-when-using-AAD-authentication-3-660x18.png 660w\" sizes=\"(max-width: 2438px) 100vw, 2438px\" \/><\/a><\/p>\n<p>Read this specific claim value<\/p>\n<p>Claim claim = principal.Claims.FirstOrDefault(c =&gt; c.Type.Contains(&#8220;objectidentifier&#8221;));<\/p>\n<p>string aadobjid = &#8220;&#8221;;<\/p>\n<p>if (claim != null)<\/p>\n<p>{<\/p>\n<p style=\"padding-left: 40px;\">aadobjid = claim.Value;<\/p>\n<p style=\"padding-left: 40px;\">log.LogInformation(&#8220;aadobjid = &#8221; + aadobjid);<\/p>\n<p>}<\/p>\n<p>Every CRM User that we create has an associated AADID stored along which is this objectid.<\/p>\n<p>Set this to the cds client object we have created for impersonation<\/p>\n<p>\/\/establish connection with CDS<\/p>\n<p>CdsServiceClient client = new CdsServiceClient(connectionString);<\/p>\n<p>if (!string.IsNullOrEmpty(aadobjid))<\/p>\n<p>{<\/p>\n<p style=\"padding-left: 40px;\">client.CallerAADObjectId = new Guid(aadobjid);<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify;\">Do note if you run a WhoAmI request \u2013 it still returns the id of the original credentials used for establishing the connection.<\/p>\n<p>However, when you create a record, you will notice that the owner of the new record is the same user that had logged in to the connector.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the earlier blog posts, we discussed setting up an Azure function with AAD authentication and then creating a custom connector for the Azure function that also requires AAD authentication to make a connection to the connector. Given that the Azure function is configured for AAD authentication in the Authentication \/ Authorization section of the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2020\/09\/impersonation-within-azure-function-or-custom-connector-when-using-aad-authentication\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":24621,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,2088],"tags":[2070,2071,2089,545],"class_list":["post-24610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-functions","category-custom-connector","tag-aad-authentication","tag-azure-function","tag-custom-connector","tag-dynamics-365-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/24610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=24610"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/24610\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/24621"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=24610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=24610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=24610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}