Hi Chris W
Welcome to the Microsoft Q&A Platform!
To control Azure Blob access in your web app using Azure App Service Authentication, you can leverage Azure AD for authentication and authorization.
- Go to App Service > Authentication in the Azure Portal.
- Select Microsoft (Azure AD).
- Use the same Azure AD tenant as your Blob Storage account.
- Add redirect URIs for your app.
- Navigate to Storage Account > Access Control (IAM).
- Storage Blob Data Reader (for read access).
- Storage Blob Data Contributor (for upload access).
- Assign these roles to Azure AD users/groups.
- Modify Your Web App to Use Azure AD Tokens
-
pip install azure-identity azure-storage-blob flask msal
- Use MSAL or
AppServiceAuthSession
to retrieve user tokens from Azure AD. - Use the retrieved token to create a
TokenCredential
forBlobServiceClient
.
from flask import Flask, request, jsonify
from azure.storage.blob import BlobServiceClient
from azure.identity import TokenCredential
app = Flask(__name__)
@app.route("/upload", methods=["POST"])
def upload_blob():
user_token = request.headers.get("Authorization").replace("Bearer ", "")
credential = TokenCredential(token=user_token)
blob_service_client = BlobServiceClient(
account_url="https://<your-storage-account>.blob.core.windows.net",
credential=credential
)
containers = blob_service_client.list_containers()
return jsonify([c.name for c in containers])
if __name__ == "__main__":
app.run(debug=True)
ref:https://learn.microsoft.com/en-us/azure/app-service/overview-authentication-authorization
https://learn.microsoft.com/en-us/azure/storage/blobs/authorize-access-azure-active-directory
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.