title: AzureServiceTokenProvider Exception: Tried to get token using Managed Service Identity. An error occurred while sending the request
body: <p>I am trying to use User Defined MSI to gain authentication token and then connect to SQL Database.</p>
  
  <p>Steps I see so far;</p>
  
  <ol>
  <li>Create a User Defined Managed Identity --> <strong>DONE</strong>.</li>
  <li>Get Authentication Token using AzureServiceTokenProvider --> <strong>This is where I get error/exception</strong>.</li>
  <li>Use the Authentication Token received using AzureServiceTokenProvider into SQLConnection. </li>
  <li>Carry out ADO.Net or EF operations.</li>
  </ol>
  
  <p>I have created a User Defined Managed Identity in my Azure Subscription. I then proceeded to write a simple code to gain access token using AzureServiceTokenProvider. Code is as follows;</p>
  
  <pre><code>static void Main(string[] args)
      {
          try
          {
              //var tokenProviderConnectionString = "RunAs=Developer; DeveloperTool=VisualStudio";
              var tokenProviderConnectionString = "RunAs=App;AppId=bbda13b3-b948-4d02-91bb-31f198729887";
  
              //var azureResource = "https://management.azure.com/";
              var azureResource = "https://database.windows.net/";
  
  
              Console.WriteLine("Attempting to gain access token from Azure...");
              var azureServiceTokenProvider = new AzureServiceTokenProvider(tokenProviderConnectionString);
              string accessToken = azureServiceTokenProvider.GetAccessTokenAsync(azureResource, "&lt;&lt;My Tenant ID&gt;&gt;").GetAwaiter().GetResult();
              Console.WriteLine("Successful");
              Console.WriteLine($"Access Token: {accessToken}");
          }
          catch (Exception ex)
          {
              Console.WriteLine("Error");
              Console.WriteLine(ex.ToString());
          }
  
      }
  </code></pre>
  
  <p>I get an error on <code>string accessToken = azureServiceTokenProvider.GetAccessTokenAsync(azureResource, "89cda43f-eb76-413b-bcbf-5c9b17542338").GetAwaiter().GetResult();</code>. </p>
  
  <p>Exception is</p>
  
  <pre><code>Parameters: Connection String: RunAs=App;AppId=bbda13b3-b948-4d02-91bb-31f198729887, Resource: https://database.windows.net/, Authority: https://login.microsoftonline.com/&lt;&lt;My Tenant ID&gt;&gt;. Exception Message: Tried to get token using Managed Service Identity. Access token could not be acquired. An error occurred while sending the request.
  </code></pre>
  
  <p>If I use <code>var tokenProviderConnectionString = "RunAs=Developer; DeveloperTool=VisualStudio";</code> as a connection string, then I am able to get an access token as it is using my Visual Studio Login as an MSI.
  But when I use <code>var tokenProviderConnectionString = "RunAs=App;AppId=bbda13b3-b948-4d02-91bb-31f198729887";</code> I get the exception mentioned above. I think probably there is a bug in AzureServiceTokenProvider or I am missing something in the request.</p>
  
  <p>Any pointers?</p>