title: azure function using app services and with access to Microsoft Dynamic 365 CRM online
body: <p>I have an Azure function that is working.  However, I am trying to also use the azure function to do authentication to get access to Microsoft Dynamic CRM.</p>
  <p>When trying to get my token is when my problem occurs.</p>
  <p>I hope someone can help me get this working.</p>
  <pre><code>using System;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Microsoft.Azure.WebJobs;
  using Microsoft.Azure.WebJobs.Extensions.Http;
  using Microsoft.Azure.WebJobs.Host;
  using Microsoft.IdentityModel.Clients.ActiveDirectory;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Client;
  using Microsoft.Xrm.Sdk.WebServiceClient;
  
  namespace LanguageFunction
  {
      public static class LanguageFunctionPlugin
      {
  
         [FunctionName(&quot;LanguageFunctionPlugin&quot;)]
         public static async Task&lt;HttpResponseMessage&gt; 
  Run([HttpTrigger(AuthorizationLevel.Function, &quot;get&quot;, &quot;post&quot;, Route = null)]HttpRequestMessage req, 
  TraceWriter log)
          {
              log.Info(&quot;C# HTTP trigger function processed a request.&quot;);
  
              // parse query parameter
             string name = req.GetQueryNameValuePairs()
                 .FirstOrDefault(q =&gt; string.Compare(q.Key, &quot;name&quot;, true) == 0)
                 .Value;
  
  
              string organizationUrl = &quot;https://xxxxxx.crm.dynamics.com&quot;;
              string api = &quot;https://xxxxxx.crm.dynamics.com/api/data/v9.1&quot;;
  
              AuthenticationParameters ap = AuthenticationParameters.CreateFromUrlAsync(
                  new Uri(api)).Result;
  
              var clientId = &quot;833ab393-56ef-6ed6-10e7-89accb8fea8b&quot;;
              var secertKey = &quot;.N:?.h6NfT3FVCNcfdvzfbZzPZguq6t3&quot;;
  
              var creds = new ClientCredential(clientId, secertKey);
              log.Info(&quot;client:&quot; + clientId);
  
              AuthenticationContext authContext = new AuthenticationContext(ap.Authority);
              var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;
  
              log.Info(&quot;Token : &quot; + token);
  
              Uri serviceUrl = new Uri(organizationUrl + @&quot;/xrmservices/2011/organization.svc/web?SdkClientVersion=v9.1&quot;);
  
              using (var sdkService = new OrganizationWebProxyClient(serviceUrl, false))
              {
                  sdkService.HeaderToken = token;
  
                  var _orgService = (IOrganizationService)sdkService != null ? (IOrganizationService)sdkService : null;
                  log.Info(&quot;Get Org Service&quot;);
              }
  
              return name == null
                  ? req.CreateResponse(HttpStatusCode.BadRequest, &quot;Please pass a name on the query string or in the request body&quot;)
                  : req.CreateResponse(HttpStatusCode.OK, &quot;Hello &quot; + name);
          }
     }
  </code></pre>
  <p>}</p>