'Azure App Service with AAD identity provider - Python & Streamlit framework for app - get logged in user
Have a web app developed in Python with the Streamlit framework. Deploying as an Azure app service. Authentication to the app is via AAD.
I'm unable to get details such as name/email address of the logged in user. Most welcome any suggestions (I've tried /.auth/me endpoint, looking at cookie sessions).
Thanks!
Solution 1:[1]
The /.auth/me endpoint gives you the information you need, i.e., it is a part of the access token (RS256 encoded) and maybe even decoded as well. You need to include the AppServiceAuthSession cookie in your get request to the endpoint.
This code snippet should work in streamlit:
import requests
from streamlit.server.server import Server
from streamlit.report_thread import get_report_ctx
session_id = get_report_ctx().session_id
session_info = Server.get_current()._get_session_info(session_id)
session_headers = session_info.ws.request.headers
ckks = session_headers['cookie']
ckkd = dict(item.split("=",1) for item in ckks.split("; "))
tokens = requests.get('https://<your_app>.azurewebsites.net/.auth/me',cookies=ckkd)
tokens = tokens.json()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Adam |