You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "This sample demonstrates a Python web application calling a Microsoft Graph that is secured using Azure Active Directory."
8
8
urlFragment: ms-identity-python-webapp
9
9
---
10
-
# Integrating Microsoft Identity Platform with a Python web application
10
+
# Integrating Microsoft Entra ID with a Python web application written in Flask
11
11
12
-
This is a Python web application that uses the Flask framework and the Microsoft identity platform to sign in users and make authenticated calls to the Microsoft Graph API.
12
+
This is a multi-purpose [Flask](https://flask.palletsprojects.com/en/3.0.x/) web app sample.
13
+
Write your app like this once, and the same implementation will support 4x2=8 scenarios.
13
14
14
-
# Configuration
15
+
|| Microsoft Entra ID | External ID | External ID with Custom Domain | Azure AD B2C |
To get started with this sample, you have two options:
22
+
## Getting Started
19
23
20
-
* Use the Azure portal to create the Azure AD applications and related objects. Follow the steps in
21
-
[Quickstart: Add sign-in with Microsoft to a Python web app](https://docs.microsoft.com/azure/active-directory/develop/web-app-quickstart?pivots=devlang-python).
22
-
* Use PowerShell scripts that automatically create the Azure AD applications and related objects (passwords, permissions, dependencies) for you, and then modify the configuration files. Follow the steps in the [App Creation Scripts README](./AppCreationScripts/AppCreationScripts.md).
24
+
### Prerequisites
23
25
24
-
## If you are configuring your B2C app
26
+
1. Have [Python](https://python.org) 3.8+ installed
or download its zip package, and then start using it or build on top of it.
30
+
1.`cd project_name`
31
+
1. Run `pip install -r requirements.txt` to install dependencies
32
+
1. Run `flask run -h localhost` and then browse to http://localhost:5000
33
+
You may need to change to a different port to match your redirect_uri setup.
25
34
26
-
This sample can also work as a B2C app. If you are using a B2C tenant, follow
27
-
[Configure authentication in a sample Python web app by using Azure AD B2C](https://learn.microsoft.com/azure/active-directory-b2c/configure-authentication-sample-python-web-app).
28
35
36
+
### How to configure and use this sample
29
37
30
-
# Deployment
38
+
<tableborder=1>
39
+
<tr>
40
+
<th></th>
41
+
<th>Microsoft Entra ID</th>
42
+
<th>Microsoft Entra External ID</th>
43
+
<th>Microsoft Entra External ID with Custom Domain</th>
44
+
<th>Azure AD B2C</th>
45
+
</tr>
31
46
32
-
Once you finish testing this web app locally, you can deploy it to your production.
33
-
You may choose any web app hosting services you want.
<td><!-- See https://github.com/github/cmark-gfm/issues/12 -->
36
50
37
-
* Follow the ["Quickstart: Deploy a Python (Django or Flask) web app to Azure App Service"](https://learn.microsoft.com/en-us/azure/app-service/quickstart-python),
38
-
but replace its sample app (which does not do user sign-in) with this web app.
51
+
Following only the step 1, 2 and 3 of this
52
+
[Quickstart: Add sign-in with Microsoft to a Python web app](https://learn.microsoft.com/entra/identity-platform/quickstart-web-app-python-sign-in?tabs=windows)
53
+
54
+
</td>
55
+
<td>
56
+
57
+
Follow only the page 1 of this [Tutorial: Prepare your customer tenant ...](https://learn.microsoft.com/entra/external-id/customers/tutorial-web-app-python-flask-prepare-tenant)
58
+
59
+
</td>
60
+
<td>
61
+
62
+
Coming soon.
63
+
64
+
</td>
65
+
<td>
66
+
67
+
Following only the step 1 and 2 (including 2.1 and 2.2) of this
68
+
[Configure authentication in a sample Python web app by using Azure AD B2C](https://learn.microsoft.com/azure/active-directory-b2c/configure-authentication-sample-python-web-app?tabs=linux)
69
+
70
+
</td>
71
+
</tr>
72
+
73
+
<tr>
74
+
<th rowspan=2>Configuration</th>
75
+
<td><!-- See https://github.com/github/cmark-gfm/issues/12 -->
76
+
77
+
Copy this [Entra ID template](.env.sample.entra-id)
78
+
as `.env` and then modify `.env` with your app's settings.
79
+
80
+
</td>
81
+
<td>
82
+
83
+
Copy this [External ID template](.env.sample.external-id)
84
+
as `.env` and then modify `.env` with your app's settings.
85
+
86
+
</td>
87
+
<td>
88
+
89
+
Copy this [External ID with Custom Domain template](.env.sample.external-id-custom-domain)
90
+
as `.env` and then modify `.env` with your app's settings.
91
+
92
+
</td>
93
+
<td>
94
+
95
+
Copy this [Azure AD B2C template](.env.sample.b2c)
96
+
as `.env` and then modify `.env` with your app's settings.
97
+
98
+
</td>
99
+
</tr>
100
+
101
+
<tr>
102
+
<td colspan=4>
103
+
104
+
Do not reverse the order of the configuration steps above.
105
+
If you put your app credentials into the template and then copy it into `.env`,
106
+
you risk accidentally committing your templates with credentials into Version Control System.
39
107
40
-
*[Configure your app's settings](https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-app-settings) to define [these environment variables](https://github.com/Azure-Samples/ms-identity-python-webapp/blob/main/.env.sample).
108
+
</td>
109
+
</tr>
41
110
111
+
<tr>
112
+
<th>Web App Sign In & Sign Out</th>
113
+
<td colspan=4>
114
+
115
+
With the basic configuration above,
116
+
you can now browse to the index page of this sample to try the sign-in/sign-out experience.
117
+
118
+
</td>
119
+
</tr>
120
+
121
+
<tr>
122
+
<th>Web App Calls a web API</th>
123
+
<td colspan=4>
124
+
125
+
Add the web API's *endpoint* into your `.env` file.
126
+
Also add the *scopes* it needs, separated by space.
127
+
The following example is the settings needed to call the Microsoft Graph API.
128
+
You may need to replace the their values with your own API endpoint and its scope..
129
+
130
+
```ini
131
+
ENDPOINT=https://graph.microsoft.com/v1.0/me
132
+
SCOPE=User.Read
133
+
```
134
+
135
+
Now restart this sample and try its "Call API" experience.
* Follow the ["Quickstart: Deploy a Python (Django or Flask) web app to Azure App Service"](https://learn.microsoft.com/en-us/azure/app-service/quickstart-python),
150
+
but replace its sample app (which does not do user sign-in) with this web app.
151
+
*[Configure your app's settings](https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-app-settings)
152
+
to define environment variables mentioned in the **Configuration** row of this table.
42
153
* If your `app_config.py` contains `SESSION_TYPE = "filesystem"`,
43
154
you shall turn on "session affinity" (a.k.a. "ARR affinity") in your
44
155
[App Service Web App's Configuration](https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-general-settings)
45
156
157
+
</td>
158
+
</tr>
159
+
160
+
</table>
161
+
162
+
163
+
## How to build this sample (or a new web project) from scratch
164
+
165
+
You can follow the
166
+
[instructions for Flask, from the underlying library](https://identity-library.readthedocs.io/en/latest/flask.html).
167
+
168
+
You can refer to the
169
+
[source code of this full sample here](https://github.com/Azure-Samples/ms-identity-python-webapp)
170
+
to pick up other minor details, such as how to modify `app.py` accordingly,
171
+
and how to add templates for the new view (and for the existing `index()` view).
0 commit comments