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
Copy file name to clipboardExpand all lines: articles/active-directory-b2c/configure-authentication-sample-python-web-app.md
+36-76Lines changed: 36 additions & 76 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ manager: CelesteDG
7
7
ms.service: active-directory
8
8
ms.workload: identity
9
9
ms.topic: reference
10
-
ms.date: 06/28/2022
10
+
ms.date: 02/28/2023
11
11
ms.author: kengaderdus
12
12
ms.subservice: B2C
13
13
ms.custom: "b2c-support"
@@ -19,7 +19,7 @@ This article uses a sample Python web application to illustrate how to add Azure
19
19
20
20
## Overview
21
21
22
-
OpenID Connect (OIDC) is an authentication protocol that's built on OAuth 2.0. You can use OIDC to securely sign users in to an application. This web app sample uses the [Microsoft Authentication Library (MSAL) for Python](https://github.com/AzureAD/microsoft-authentication-library-for-python). The MSAL for Python simplifies adding authentication and authorization support to Python web apps.
22
+
OpenID Connect (OIDC) is an authentication protocol that's built on OAuth 2.0. You can use OIDC to securely sign users in to an application. This web app sample uses the [identity package for Python](https://pypi.org/project/identity/) to simplify adding authentication and authorization support to Python web apps.
23
23
24
24
The sign-in flow involves the following steps:
25
25
@@ -29,17 +29,11 @@ The sign-in flow involves the following steps:
29
29
1. After users sign in successfully, Azure AD B2C returns an ID token to the app.
30
30
1. The app exchanges the authorization code with an ID token, validates the ID token, reads the claims, and then returns a secure page to users.
*[Visual Studio Code](https://code.visualstudio.com/) or another code editor
42
-
*[Python](https://www.python.org/downloads/) 3.9 or above
34
+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
35
+
- If you don't have one already, [create an Azure AD B2C tenant](tutorial-create-tenant.md) that is linked to your Azure subscription.
36
+
-[Python 3.7+](https://www.python.org/downloads/)
43
37
44
38
## Step 1: Configure your user flow
45
39
@@ -90,37 +84,29 @@ Extract the sample file to a folder where the total length of the path is 260 or
90
84
In the project's root directory, follow these steps:
91
85
92
86
1. Rename the *app_config.py* file to *app_config.py.OLD*.
93
-
1. Rename the *app_config_b2c.py* file to *app_config.py*.
94
-
95
-
Open the *app_config.py* file. This file contains information about your Azure AD B2C identity provider. Update the following app settings properties:
96
-
97
-
|Key |Value |
98
-
|---------|---------|
99
-
|`b2c_tenant`| The first part of your Azure AD B2C [tenant name](tenant-management-read-tenant-name.md#get-your-tenant-name) (for example, `contoso`).|
100
-
|`CLIENT_ID`| The web API application ID from [step 2.1](#step-21-register-the-app).|
101
-
|`CLIENT_SECRET`| The client secret value you created in [step 2.2](#step-22-create-a-web-app-client-secret). To help increase security, consider storing it instead in an environment variable, as recommended in the comments. |
102
-
|`*_user_flow`|The user flows or custom policy you created in [step 1](#step-1-configure-your-user-flow).|
103
-
|||
104
-
105
-
Your final configuration file should look like the following Python code:
CLIENT_ID="11111111-1111-1111-1111-111111111111"# Application (client) ID of app registration
117
-
118
-
CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxx"# Placeholder - for use ONLY during testing.
119
-
```
87
+
1. Rename the *app_config_b2c.py* file to *app_config.py*. This file contains information about your Azure AD B2C identity provider.
88
+
89
+
1. Create an `.env` file in the root folder of the project using `.env.sample.b2c` as a guide.
90
+
91
+
```shell
92
+
FLASK_DEBUG=True
93
+
TENANT_NAME=<tenant name>
94
+
CLIENT_ID=<client id>
95
+
CLIENT_SECRET=<client secret>
96
+
SIGNUPSIGNIN_USER_FLOW=B2C_1_profile_editing
97
+
EDITPROFILE_USER_FLOW=B2C_1_reset_password
98
+
RESETPASSWORD_USER_FLOW=B2C_1_signupsignin1
99
+
```
120
100
121
-
> [!IMPORTANT]
122
-
> As noted in the code snippet comments, we recommend that you *do not store secrets in plaintext* in your application code. The hard-coded variable is used in the code sample *for convenience only*. Consider using an environment variable or a secret store, such as an Azure key vault.
101
+
|Key |Value |
102
+
|---------|---------|
103
+
|`TENANT_NAME`| The first part of your Azure AD B2C [tenant name](tenant-management-read-tenant-name.md#get-your-tenant-name) (for example, `contoso`). |
104
+
|`CLIENT_ID`| The web API application ID from [step 2.1](#step-21-register-the-app).|
105
+
|`CLIENT_SECRET`| The client secret value you created in [step 2.2](#step-22-create-a-web-app-client-secret). |
106
+
|`*_USER_FLOW`|The user flows you created in [step 1](#step-1-configure-your-user-flow).|
107
+
|||
123
108
109
+
The environment variables are referenced in*app_config.py*, and are kept in a separate *.env* file to keep them out of source control. The provided *.gitignore* file prevents the *.env* file from being checked in.
124
110
125
111
## Step 5: Run the sample web app
126
112
@@ -157,11 +143,9 @@ CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxx" # Placeholder - for use ONLY during t
157
143
The console window displays the port number of the locally running application:
158
144
159
145
```console
160
-
* Serving Flask app "app" (lazy loading)
161
-
* Environment: production
146
+
* Debug mode: on
162
147
WARNING: This is a development server. Do not use it in a production deployment.
163
148
Use a production WSGI server instead.
164
-
* Debug mode: off
165
149
* Running on `http://localhost:5000/` (Press CTRL+C to quit)
166
150
```
167
151
@@ -190,7 +174,7 @@ To enable your app to sign in with Azure AD B2C and call a web API, you must reg
190
174
191
175
The app registrations and the application architecture are described in the following diagrams:
192
176
193
-

177
+

@@ -208,58 +192,34 @@ The app registrations and the application architecture are described in the foll
208
192
209
193
### Step 6.4: Configure your web API
210
194
211
-
This sample acquires an access token with the relevant scopes, which the web app can use for a web API. To call a web API from the code, use an existing web API or create a new one. For more information, see [Enable authentication in your own web API by using Azure AD B2C](enable-authentication-web-api.md).
195
+
This sample acquires an access token with the relevant scopes, which the web app can use fora web API. This sample itself does *not* act as a web API. Instead, you must use an existing web API or create a new one. For a tutorial on creating a web APIin your B2C tenant, see [Enable authentication in your own web API by using Azure AD B2C](enable-authentication-web-api.md).
212
196
213
197
### Step 6.5: Configure the sample app with the web API
214
198
215
199
Open the *app_config.py* file. This file contains information about your Azure AD B2C identity provider. Update the following properties of the app settings:
216
200
217
201
|Key |Value |
218
202
|---------|---------|
219
-
|`ENDPOINT`| The URI of your web API (for example, `https://localhost:5000/getAToken`).|
220
-
|`SCOPE`| The web API [scopes](#step-62-configure-scopes) that you created.|
203
+
|`ENDPOINT`| The URI of your web API (for example, `https://localhost:6000/hello`).|
204
+
|`SCOPE`| The web API [scopes](#step-62-configure-scopes) that you created (for example, `["https://contoso.onmicrosoft.com/tasks-api/tasks.read", https://contoso.onmicrosoft.com/tasks-api/tasks.write"]`).|
221
205
|||
222
206
223
-
Your final configuration file should look like the following Python code:
1. In your console or terminal, switch to the directory that contains the sample.
250
-
1.Stop the app. and then rerun it.
251
-
1. Select **Call Microsoft Graph API**.
210
+
1. If the app isn't still running, restart it using the command from Step 5.
211
+
1. Select **Call a downstream API**.
252
212
253
-

213
+

254
214
255
215
## Step 7: Deploy your application
256
216
257
217
In a production application, the app registration redirect URI is ordinarily a publicly accessible endpoint where your app is running, such as `https://contoso.com/getAToken`.
258
218
259
219
You can add and modify redirect URIs in your registered applications at any time. The following restrictions apply to redirect URIs:
260
220
261
-
* The reply URL must begin with the scheme `https`.
262
-
* The reply URL is case-sensitive. Its case must match the case of the URL path of your running application.
221
+
* The redirect URL must begin with the scheme `https`.
222
+
* The redirect URL is case-sensitive. Its case must match the case of the URL path of your running application.
263
223
264
224
## Next steps
265
225
* Learn how to [Configure authentication options in a Python web app by using Azure AD B2C](enable-authentication-python-web-app-options.md).
0 commit comments