There is some great guidance on using Azure Functions with SharePoint Online cookie authentication here: https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/guidance/connect-to-api-secured-with-aad
Unfortunately the guidance doesn't cover the situation where the cookie has expired and the authentication fails (for example if the browser is left open for a long time after initial sign-in to SharePoint).
Here are some (hopefully) helpful classes for implementing Azure Functions with SPO cookie authentication that also allow handling the expired SPO cookie and attaching CORS headers in C#.