title: User Assigned Identities with App Service and Azure SQL does that work?
body: <p>I am trying to get App Service to connect with Azure Sql database. I can git is nicely work with System Assigned Identities with the same code, but I prefer to use User Assigned Identities (UAI), but I cannot get it work.</p>
  
  <p>Steps which I do:</p>
  
  <ol>
  <li>Created a UAI via the portal, name of the UAI "uai-dev-appname-001"</li>
  <li>At the Identity tab of the Azure App Service I selected 'User Assigned Identity' and selected the UAI made in the previous step.</li>
  <li>Ran the following SQL CMD</li>
  </ol>
  
  <pre><code>CREATE USER [uai-dev-appname-001] FROM EXTERNAL PROVIDER
  ALTER ROLE db_datareader ADD MEMBER [uai-dev-appname-001]
  ALTER ROLE db_datawriter ADD MEMBER [uai-dev-appname-001]
  </code></pre>
  
  <ol start="4">
  <li>Set Connectionstring in the ASP.NET to: 
  
  <blockquote>
    <p>Data Source=sqlsrv-name-dev-001.database.windows.net; Initial Catalog=sqldb-name-dev-001;</p>
  </blockquote></li>
  <li>Using the following code in mine ASP.NET Core:</li>
  </ol>
  
  <pre><code>SqlConnection connection = new SqlConnection
  {
     ConnectionString = configuration.GetConnectionString("nameDatabase")
  };
  AzureServiceTokenProvider provider = new AzureServiceTokenProvider();
  var token = provider.GetAccessTokenAsync("https://database.windows.net/").Result;
  connection.AccessToken = token;
  </code></pre>
  
  <ol start="6">
  <li>Deploy to Azure App Service and watched the URL. The result is: error 500.30</li>
  <li>Looking in the Application Event Log:
  
  <blockquote>
    <p>Unhandled exception. System.AggregateException: One or more errors occurred. (Parameters: Connection String: [No connection string specified], Resource: <a href="https://database.windows.net" rel="noreferrer">https://database.windows.net</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://database.windows.net" rel="noreferrer">https://database.windows.net</a>, Authority: . Exception Message: Tried to get token using Managed Service Identity. Access token could not be acquired. Received a non-retryable error. MSI ResponseCode: BadRequest, Response: {"StatusCode":400,"Message":"No MSI found for specified ClientId/ResourceId.","CorrelationId":"a68bf757-518a-42e1-85a9-342320d39b5a"}
    Parameters: Connection String: [No connection string specified], Resource: <a href="https://database.windows.net" rel="noreferrer">https://database.windows.net</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 "D:\local\LocalAppData.IdentityService\AzureServiceAuth\tokenprovider.json"
    Parameters: Connection String: [No connection string specified], Resource: <a href="https://database.windows.net" rel="noreferrer">https://database.windows.net</a>, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. 'az' is not recognized as an internal or external command,
    operable program or batch file.</p>
  </blockquote></li>
  </ol>
  
  <p>The most interesting part in IMO is:</p>
  
  <blockquote>
    <p>Response: {"StatusCode":400,"Message":"No MSI found for specified ClientId/ResourceId.","CorrelationId":"a68bf757-518a-42e1-85a9-342320d39b5a"}</p>
  </blockquote>
  
  <p>Mine question are:</p>
  
  <ul>
  <li>Does User Assigned Identies work with Azure SQL?</li>
  <li>If so what do I do wrong?</li>
  <li>Does someone has a working example.</li>
  </ul>