Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
Hi @Jonathan Jasica,
Even though you are on a Basic Plan with only one instance running (as shown in your screenshot), here’s the key insight:
The problem isn’t due to load balancing across instances; it’s due to how Flask handles sessions (local filesystem) and how Azure handles requests during authentication redirects.
Azure may spin down and spin up your app between requests, especially during cold starts, restarts, or heavy resource contention. Since the session is stored in a temp directory, it might get lost even on the same instance. Also, if your app is running in multiple processes (e.g., due to gunicorn/WSGI configurations), the local session file may not be visible across them.
During an OAuth flow, the request typically goes through several redirects (e.g., browser → Microsoft Login → redirect URI). These redirects can trigger a new app worker process or temporarily route through different internal endpoints, even on a single instance. Since Flask stores sessions on local disk by default, and these sessions are not always accessible across all request contexts, the session can appear “lost.”
- The
SESSION_COOKIE_SAMESITE="None"setting requires the cookie to be markedSecureand served over HTTPS which you are doing, but cross-domain or HTTPS redirect behaviors might still interfere. - Azure App Service handles authentication and traffic via front-end proxies/load balancers, which can impact local-only session storage.
To make your Flask app stateless and avoid depending on the local file system, you should switch your session backend to Redis. This ensures that the session persists across any number of requests, redirects, and internal routing behaviors.
Install Flask-Session and Redis libraries:
pip install Flask-Session redis
Update your Flask config:
from flask import Flask
from flask_session import Session
import redis
app = Flask(__name__)
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host='your-redis-hostname', port=6380, password='your-redis-password', ssl=True)
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'None'
app.config['SESSION_COOKIE_HTTPONLY'] = True
Session(app)
Deploy your app with these settings and configure an Azure Cache for Redis instance.
Reference:
https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#session-behavior
https://learn.microsoft.com/en-us/azure/app-service/overview-hosting-plans
If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.
Let me know if you have any further Queries.