title: Azure Function - Get Token from DefaultCredentials \ Managed Identity
body: <p>I am working with an Azure Function that needs to authenticate into an API /APP Service with using JWT.</p>
  <p>I have been looking around a LOT of examples and settled on this as being the most appropriate</p>
  <pre><code>public string GenerateToken()
  {
      var credential = new Azure.Identity.DefaultAzureCredential();
      var token = credential.GetToken(new Azure.Core.TokenRequestContext(new[] { &quot;https://management.azure.com/&quot; }));
      return token.Token;
  }
  </code></pre>
  <p>Example born from here <a href="https://docs.microsoft.com/en-us/dotnet/api/azure.core.tokencredential?view=azure-dotnet" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/api/azure.core.tokencredential?view=azure-dotnet</a></p>
  <p>I have also tried to use this <a href="https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-c" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-c</a></p>
  <p>Looking here, this is similar to my issue, but because its not using Managed Identity the company I am working for say this is a no go.
  <a href="https://stackoverflow.com/questions/58209315/create-azure-bearer-token-from-azure-function">Create azure bearer token from azure function</a></p>
  <p>I believe the problem is the scope, but I cannot find the right scope to get into my app service.</p>
  <p>I have also tried api:// , the resource url and more</p>
  <p>Any and all help gratefully received.</p>