title: Use managed identities in Azure DevOps build pipeline
body: <p>I managed to get the below code to work (<a href="https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-msi" rel="noreferrer">complete code here</a>) to use Azure managed identity to authenticate (via Visual Studio) and have access to Azure storage account without using credentials.</p>
  
  <pre><code>const string storageResource = "https://storage.azure.com/";
  
  var authResult = await azureServiceTokenProvider.GetAuthenticationResultAsync(storageResource, cancellationToken: cancellationToken);
  
  </code></pre>
  
  <p>The code managed to find my user logged in to Visual Studio and uses it to get the token and all goes well.</p>
  
  <p>However, this code is executed as part of a library integration tests in an Azure DevOps build pipeline.</p>
  
  <p>I found the service principal created when I created the service connection to Azure in Azure DevOps and gave it the same <code>Storage Blob Data Contributor</code> role hoping that Azure DevOps would use it to run the code but had no success.</p>
  
  <p>So my question is:</p>
  
  <p>How do I get code that runs in Azure DevOps build pipeline to be able to authenticate using the <code>AzureServiceTokenProvider</code>?</p>
  
  <p>BTW, the error message:</p>
  
  <blockquote>
    <p>Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException
    : Parameters: Connection String: [No connection string specified],
    Resource: <a href="https://storage.azure.com/" rel="noreferrer">https://storage.azure.com/</a>, Authority: . Exception Message:
    Tried the following 3 methods to get an access token, but none of them
    worked. Parameters: Connection String: [No connection string
    specified], Resource: <a href="https://storage.azure.com/" rel="noreferrer">https://storage.azure.com/</a>, Authority: .
    Exception Message: Tried to get token using Managed Service Identity.
    Access token could not be acquired. Failed after 5 retries. MSI
    ResponseCode: BadRequest, Response:
    {"error":"invalid_request","error_description":"Identity not found"}
    Parameters: Connection String: [No connection string specified],
    Resource: <a href="https://storage.azure.com/" rel="noreferrer">https://storage.azure.com/</a>, Authority: . Exception Message:
    Tried to get token using Visual Studio. Access token could not be
    acquired. Visual Studio Token provider file not found at
    "C:\Users\VssAdministrator\AppData\Local.IdentityService\AzureServiceAuth\tokenprovider.json"
    Parameters: Connection String: [No connection string specified],
    Resource: <a href="https://storage.azure.com/" rel="noreferrer">https://storage.azure.com/</a>, Authority: . Exception Message:
    Tried to get token using Azure CLI. Access token could not be
    acquired. ERROR: Please run 'az login' to setup account.</p>
    
    <p>TearDown : System.NullReferenceException : Object reference not set to
    an instance of an object.</p>
  </blockquote>