Skip to content

Commit dce9db1

Browse files
authored
Merge pull request #97 from Azure-Samples/app-service-howto
How to deploy to App Service
2 parents 27d6dfd + f5161e3 commit dce9db1

10 files changed

+67
-17
lines changed

.env.sample

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
FLASK_DEBUG=True
1+
# Note: If you are using Azure App Service, go to your app's Configuration,
2+
# and then set the following values into your app's "Application settings".
3+
4+
CLIENT_ID=<client id>
5+
CLIENT_SECRET=<client secret>
6+
27
# Expects a full tenant id such as "contoso.onmicrosoft.com", or its GUID
38
# Or leave it undefined if you are building a multi-tenant app
49
#TENANT_ID=<tenant id>
5-
CLIENT_ID=<client id>
6-
CLIENT_SECRET=<client secret>

.env.sample.b2c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
FLASK_DEBUG=True
2-
# Expects the display name such as "contoso"
3-
TENANT_NAME=<tenant name>
1+
# Note: If you are using Azure App Service, go to your app's Configuration,
2+
# and then set the following values into your app's "Application settings".
43
CLIENT_ID=<client id>
54
CLIENT_SECRET=<client secret>
5+
# Expects the display name such as "contoso"
6+
TENANT_NAME=<tenant name>
67
SIGNUPSIGNIN_USER_FLOW=B2C_1_profile_editing
78
EDITPROFILE_USER_FLOW=B2C_1_reset_password
8-
RESETPASSWORD_USER_FLOW=B2C_1_signupsignin1
9+
RESETPASSWORD_USER_FLOW=B2C_1_signupsignin1

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ To get started with this sample, you have two options:
1717
[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).
1818
* 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).
1919

20+
# Deployment
21+
22+
Once you finish testing this web app locally, you can deploy it to your production.
23+
You may choose any web app hosting services you want.
24+
Here we will describe how to deploy it to
25+
[Azure App Service](https://azure.microsoft.com/en-us/products/app-service).
26+
27+
* 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),
28+
but replace its sample app (which does not do user sign-in) with this web app.
29+
30+
* In particular, if you choose to ["deploy using Local Git" in "step 3 - Deploy your application code to Azure"](https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cwindows%2Cazure-cli%2Clocal-git-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli#3---deploy-your-application-code-to-azure),
31+
an [application-scope credential](https://learn.microsoft.com/en-us/azure/app-service/deploy-configure-credentials?tabs=portal#appscope)
32+
will be automatically created with the shape as `your_app_name\$your_app_name`.
33+
But your actual git username is only the `$your_app_name` part.
34+
35+
* [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/master/.env.sample).
36+
37+
2038
## Contributing
2139

2240
If you find a bug in the sample, please raise the issue on [GitHub Issues](../../issues).

app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def logout():
4848

4949
@app.route("/")
5050
def index():
51+
if not (app.config["CLIENT_ID"] and app.config["CLIENT_SECRET"]):
52+
# This check is not strictly necessary.
53+
# You can remove this check from your production code.
54+
return render_template('config_error.html')
5155
if not auth.get_user():
5256
return redirect(url_for("login"))
5357
return render_template('index.html', user=auth.get_user(), version=identity.__version__)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Flask>=2
1+
# Our how-to docs will use --debug parameter which is only available in Flask 2.2+
2+
Flask>=2.2
23
# If Flask-Session is not maintained in future, Flask-Session2 should work as well
34
Flask-Session>=0.3.2,<0.5
45
werkzeug>=2

templates/auth_error.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
65
{% if config.get("B2C_RESET_PASSWORD_AUTHORITY") and "AADB2C90118" in result.get("error_description") %} <!-- This will be reached when user forgot their password -->
76
<!-- See also https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-policies#linking-user-flows -->
87
<meta http-equiv="refresh" content='0;{{config.get("B2C_RESET_PASSWORD_AUTHORITY")}}?client_id={{config.get("CLIENT_ID")}}'>
98
{% endif %}
9+
<title>Microsoft Identity Python Web App: Error</title>
1010
</head>
1111
<body>
1212
<h2>Login Failure</h2>
@@ -18,4 +18,3 @@ <h2>Login Failure</h2>
1818
<a href="{{ url_for('index') }}">Homepage</a>
1919
</body>
2020
</html>
21-

templates/config_error.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Microsoft Identity Python Web App: Error</title>
6+
</head>
7+
<body>
8+
<h2>Config Missing</h2>
9+
<p>
10+
Almost there. Did you forget to set up
11+
<a target=_blank
12+
href="https://learn.microsoft.com/azure/active-directory/develop/web-app-quickstart?pivots=devlang-python#step-5-configure-the-sample-app">
13+
necessary environment variables</a> for your deployment?
14+
</p>
15+
<hr>
16+
<a href="{{ url_for('index') }}">Homepage</a>
17+
</body>
18+
</html>

templates/display.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5+
<title>Microsoft Identity Python Web App: API</title>
56
</head>
67
<body>
78
<a href="javascript:window.history.go(-1)">Back</a> <!-- Displayed on top of a potentially large JSON response, so it will remain visible -->

templates/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5+
<title>Microsoft Identity Python Web App: Index</title>
56
</head>
67
<body>
78
<h1>Microsoft Identity Python Web App</h1>
89
<h2>Welcome {{ user.get("name") }}!</h2>
910

11+
<ul>
1012
{% if config.get("ENDPOINT") %}
1113
<li><a href='/call_downstream_api'>Call a downstream API</a></li>
1214
{% endif %}
@@ -16,6 +18,8 @@ <h2>Welcome {{ user.get("name") }}!</h2>
1618
{% endif %}
1719

1820
<li><a href="/logout">Logout</a></li>
21+
</ul>
22+
1923
<hr>
2024
<footer style="text-align: right">Powered by Identity Web {{ version }}</footer>
2125
</body>

templates/login.html

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5+
<title>Microsoft Identity Python Web App: Login</title>
56
</head>
67
<body>
78
<h1>Microsoft Identity Python Web App</h1>
89

910
{% if user_code %}
1011
<ol>
11-
<li>To sign in, type <b>{{ user_code }}</b> into
12-
<a href='{{ auth_uri }}' target=_blank>{{ auth_uri }}</a>
13-
to authenticate.
14-
</li>
15-
<li>And then <a href="{{ url_for('auth_response') }}">proceed</a>.</li>
12+
<li>To sign in, type <b>{{ user_code }}</b> into
13+
<a href='{{ auth_uri }}' target=_blank>{{ auth_uri }}</a>
14+
to authenticate.
15+
</li>
16+
<li>And then <a href="{{ url_for('auth_response') }}">proceed</a>.</li>
1617
</ol>
1718
{% else %}
18-
<li><a href='{{ auth_uri }}'>Sign In</a></li>
19+
<ul><li><a href='{{ auth_uri }}'>Sign In</a></li></ul>
1920
{% endif %}
2021

2122
{% if config.get("B2C_RESET_PASSWORD_AUTHORITY") %}
22-
<li><a href="{{config.get('B2C_RESET_PASSWORD_AUTHORITY')}}?client_id={{config.get('CLIENT_ID')}}">Reset Password</a></li>
23+
<a href="{{config.get('B2C_RESET_PASSWORD_AUTHORITY')}}?client_id={{config.get('CLIENT_ID')}}">Reset Password</a>
2324
{% endif %}
2425

2526
<hr>

0 commit comments

Comments
 (0)