<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Afterthoughts: Showerthoughts, afterthoughts and others</title>
<link href="https://hetland.co/afterthoughts/"/>
<link rel="self" href="https://hetland.co/afterthoughts/feed.xml"/>
<id>https://hetland.co/afterthoughts/</id>
<updated>2026-01-01T12:00:00Z</updated>
<author><name>Øyvind Storesund Hetland</name></author>
<entry>
  <title>On-Behalf-Of auth in Azure Container Apps with built-in Easy Auth</title>
  <link href="https://hetland.co/afterthoughts/easy-auth-obo-container-apps/"/>
  <id>https://hetland.co/afterthoughts/easy-auth-obo-container-apps/</id>
  <updated>2026-01-01T12:00:00Z</updated>
  <summary>Getting a Graph access token handed to your app by the Easy Auth sidecar, and the three details that cost me days.</summary>
  <content type="html">&lt;p&gt;For a recent project I needed an easy-to-maintain, no-worries setup where an application running in an Azure Container App could access Microsoft Graph (SharePoint) on behalf of the user.&lt;/p&gt;
&lt;p&gt;I realised that I could let the built-in auth sidecar (Easy Auth) do this for me by handing the app an access token directly, but I ran into problems with the setup. Quite a few hours later it works wonderfully, and I wanted to share, in case a search crawler finds it, how I made it work and why I found it challenging.&lt;/p&gt;
&lt;h2&gt;On-behalf-of auth (OBO)&lt;/h2&gt;
&lt;p&gt;Reading about &lt;a href=&quot;https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-on-behalf-of-flow&quot; rel=&quot;noopener&quot;&gt;the OBO flow in Microsoft’s documentation&lt;/a&gt;, and seeing how easily it could be set up in &lt;a href=&quot;https://davidrosevear.dev/blog/token-store-and-obo-auth-in-container-apps&quot; rel=&quot;noopener&quot;&gt;this post on the token store and OBO in Container Apps&lt;/a&gt; (it summarises it all and has some interesting references, worth a read), I believed this would be easy-peasy. All I needed to do was request a scope in the Entra ID app registration and in my application, for an access token to e.g. Graph (like &lt;code&gt;https://graph.microsoft.com/Sites.Read.All&lt;/code&gt;). Then the auth sidecar would hand me the token in the HTTP headers, and I’d be good to go.&lt;/p&gt;
&lt;p&gt;It turned out to be quite a headache, thanks to finicky command syntax, broken or missing documentation and more.&lt;/p&gt;
&lt;h2&gt;A quick recipe that works&lt;/h2&gt;
&lt;p&gt;Instead of going through everything that didn’t work, here is what does.&lt;/p&gt;
&lt;h3&gt;The problem&lt;/h3&gt;
&lt;p&gt;When you use Azure Container Apps with built-in authentication, by default:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;user information is provided in headers (&lt;code&gt;X-MS-CLIENT-PRINCIPAL-NAME&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;access tokens are not exposed&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;and without the access token you cannot call Microsoft Graph (SharePoint, OneDrive and so on).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;The solution in four steps&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Configure authentication to request the Microsoft Graph scopes, plus &lt;code&gt;offline_access&lt;/code&gt; to get a refresh token.&lt;/li&gt;
&lt;li&gt;Make sure the app registration has the right API permissions (I don’t go through this here).&lt;/li&gt;
&lt;li&gt;Configure Easy Auth so that the tokens are actually fetched, stored and exposed. As of 1 January 2026 I believe this &lt;strong&gt;cannot&lt;/strong&gt; be done by clicking in the Azure Portal.&lt;/li&gt;
&lt;li&gt;The access token then arrives in the &lt;code&gt;X-MS-TOKEN-AAD-ACCESS-TOKEN&lt;/code&gt; header, and is refreshed for you.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Step by step&lt;/h3&gt;
&lt;p&gt;Enable authentication on your existing Container App, in the Portal or with the CLI:&lt;/p&gt;
&lt;pre data-lang=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Set your variables&lt;/span&gt;
APP_NAME=&amp;quot;your-container-app-name&amp;quot;
RESOURCE_GROUP=&amp;quot;your-resource-group&amp;quot;
CLIENT_ID=&amp;quot;your-client-id&amp;quot;
CLIENT_SECRET=&amp;quot;your-client-secret&amp;quot;
TENANT_ID=&amp;quot;your-tenant-id&amp;quot;
ISSUER_URL=&amp;quot;https://login.microsoftonline.com/${TENANT_ID}/v2.0&amp;quot;

&lt;span class=&quot;c&quot;&gt;# Configure Microsoft authentication&lt;/span&gt;
az containerapp auth microsoft update \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP \
  --client-id $CLIENT_ID \
  --client-secret $CLIENT_SECRET \
  --issuer $ISSUER_URL \
  --tenant-id $TENANT_ID \
  --yes&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, turn on the token store. Unlike App Service, Container Apps does &lt;strong&gt;not&lt;/strong&gt; have it on by default.&lt;/p&gt;
&lt;p&gt;The SAS URL must point to a Blob Storage &lt;strong&gt;container&lt;/strong&gt;, not the storage account: a container-level SAS with read, write, create and delete permissions.&lt;/p&gt;
&lt;pre data-lang=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Critical: the token store must be turned on by hand in Container Apps&lt;/span&gt;
az containerapp secret set \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP \
  --secrets 'tokenstore-sas-url=CONTAINER_SAS_URL_HERE'

az containerapp auth update \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP \
  --sas-url-secret-name tokenstore-sas-url \
  --token-store true&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now update the configuration so the sidecar asks for the Graph scopes when the user logs in:&lt;/p&gt;
&lt;pre data-lang=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Critical: this syntax is the one that works.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Most sources online get it wrong (loginParameters[0]=scope=...).&lt;/span&gt;
az containerapp auth update \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP \
  --set 'identityProviders.azureActiveDirectory.login.loginParameters=[scope=openid profile email https://graph.microsoft.com/User.Read https://graph.microsoft.com/Sites.Read.All https://graph.microsoft.com/Files.Read.All offline_access]'&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To check the whole configuration:&lt;/p&gt;
&lt;pre data-lang=&quot;bash&quot;&gt;&lt;code&gt;az containerapp auth show \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Hot tip:&lt;/strong&gt; in the Portal or the CLI you can stream the logs of the auth sidecar too. That tells you whether authentication works at all, and surfaces some really finicky errors.&lt;/p&gt;
&lt;p&gt;Some of these changes restart the auth sidecar automatically, but sometimes that fails, and sometimes the app itself needs a restart as well. I have also seen the sidecar take a few minutes to find the token-store container. All of which makes debugging slow.&lt;/p&gt;
&lt;h2&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;I struggled with the Azure CLI rejecting commands I found online, refusing to update the scopes from an uploaded JSON file, and a SAS token of the wrong type. Pair that with a slow debugging cycle of waiting for every change and redeploy, and an error easily takes a few days to fix.&lt;/p&gt;
&lt;p&gt;I now have this working in several apps, where different Python backends use the access token for delegated access to e.g. SharePoint, and fetch a new one with the refresh token when it expires.&lt;/p&gt;
&lt;p&gt;Hope that helps someone. Good luck!&lt;/p&gt;
</content>
</entry>
</feed>
