Skip to content

Commit a7b98a4

Browse files
committed
fix merge conflicts
1 parent 0546a6d commit a7b98a4

File tree

5 files changed

+44
-64
lines changed

5 files changed

+44
-64
lines changed

.openpublishing.redirection.msal-python-conceptual.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"source_path_from_root": "/msal-python-conceptual/overview-msal",
55
"redirect_url": "/msal-python-conceptual/index",
66
"redirect_document_id": true
7+
},
8+
{
9+
"source_path_from_root": "/msal-python-conceptual/best-practices.md",
10+
"redirect_url": "/entra/msal/python",
11+
"redirect_document_id": true
712
}
813
]
914
}

msal-python-conceptual/advanced/best-practices.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

msal-python-conceptual/advanced/wam.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ manager: CelesteDG
77
ms.service: msal
88
ms.subservice: msal-python
99
ms.topic: conceptual
10-
ms.date: 02/07/2024
10+
ms.date: 04/24/2025
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
1313
---
1414

1515
# Using MSAL Python with Web Account Manager
1616

17-
If you are building a Windows application, you might consider simplifying how users authenticate with the help of an _authentication broker_ - the [Web Account Manager](/windows/uwp/security/web-account-manager) (WAM).
17+
If you are building a Windows application, you might consider simplifying how users authenticate with the help of an *authentication broker*. [Web Account Manager](/windows/uwp/security/web-account-manager) (WAM) is an authentication broker that works with MSAL Python. WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
1818

19-
>[!NOTE]
20-
>WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
21-
22-
To learn more about the benefits of using an authentication broker, refer to [What is a broker](/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam#what-is-a-broker) in the MSAL.NET documentation.
19+
For more information on the benefits of using an authentication broker, see [What is a broker](/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam#what-is-a-broker) in the MSAL.NET documentation.
2320

2421
## Usage
2522

@@ -29,13 +26,9 @@ To use the broker, you will need to install the broker-related packages in addit
2926
pip install msal[broker]>=1.20,<2
3027
```
3128

32-
>[!IMPORTANT]
33-
>If broker-related packages are not installed and you will try to use the authentication broker, you will get an error: `ImportError: You need to install dependency by: pip install "msal[broker]>=1.20,<2"`.
34-
35-
Next, you will need to instantiate a new [`PublicClientApplication`](xref:msal.application.PublicClientApplication) and set `enable_broker_on_windows` to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window.
29+
If broker-related packages aren't installed and you try to use the authentication broker, you will get the error *ImportError: You need to install dependency by: pip install "msal[broker]>=1.20,<2"*.
3630

37-
>[!IMPORTANT]
38-
>If you are writing a cross-platform application, you will also need to use `enable_broker_on_mac`, as outlined in the [Using MSAL Python with an Authentication Broker on macOS](macos-broker.md) article.
31+
Next, instantiate a new [*PublicClientApplication*](xref:msal.application.PublicClientApplication) and set *enable_broker_on_windows* to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window. If you are writing a cross-platform application, you will also need to use *enable_broker_on_mac*, as outlined in the [Using MSAL Python with an Authentication Broker on macOS](macos-broker.md) article.
3932

4033
```python
4134
from msal import PublicClientApplication
@@ -46,27 +39,26 @@ app = PublicClientApplication(
4639
enable_broker_on_windows=True)
4740
```
4841

49-
You can now acquire a token by calling [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) and specifying a parent window handle through `parent_window_handle`:
42+
You can now acquire a token by calling [*acquire_token_interactive*](xref:msal.application.PublicClientApplication.acquire_token_interactive) and specifying a parent window handle through *parent_window_handle*:
5043

5144
```python
5245
result = app.acquire_token_interactive(["User.ReadBasic.All"],
5346
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
5447
```
5548

56-
A parent window handle is required by WAM to ensure that the dialog is shown correctly on top of the requesting window. MSAL does not infer this directly due to the fact that there are many variables that might influence what window WAM needs to bind to, and developers building applications are best suited to decide what window that should be.
49+
A parent window handle is required by WAM to ensure that the dialog is shown correctly on top of the requesting window. MSAL doesn't infer this directly due to the fact that there are many variables that might influence what window WAM needs to bind to, and developers building applications are best suited to decide what window that should be.
5750

58-
For console applications, MSAL makes it easy by offering an out-of-the-box solution to getting the window handle for the terminal - [`CONSOLE_WINDOW_HANDLE`](xref:msal.application.PublicClientApplication.CONSOLE_WINDOW_HANDLE). For desktop applications, additional work with the Windows API might be required to [get the window handle](/windows/apps/develop/ui-input/retrieve-hwnd). Helper packages, like [pywin32](https://pypi.org/project/pywin32/) can help with API calls.
51+
For console applications, MSAL makes it easy by offering an out-of-the-box solution to getting the window handle for the terminal - [*CONSOLE_WINDOW_HANDLE*](xref:msal.application.PublicClientApplication.CONSOLE_WINDOW_HANDLE). For desktop applications, more work with the Windows API might be required to [get the window handle](/windows/apps/develop/ui-input/retrieve-hwnd). Helper packages, like [pywin32](https://pypi.org/project/pywin32/) can help with API calls.
5952

6053
Before executing your application, make sure that you configure the redirect URL for the desktop app:
6154

62-
>[!IMPORTANT]
63-
>To use the Windows broker, your application needs to have the correct redirect URL configured in the Azure Portal, in the shape of:
64-
>
65-
>```bash
66-
>ms-appx-web://microsoft.aad.brokerplugin/YOUR_CLIENT_ID
67-
>```
68-
>
69-
>If the redirect URL is not configured, you will get a `broker_error` similar to `(pii). Status: Response_Status.Status_ApiContractViolation, Error code: 3399614473, Tag: 557973642`.
55+
To use the Windows broker, your application needs to have the correct redirect URL configured in the Azure Portal, in the shape of:
56+
57+
```bash
58+
ms-appx-web://microsoft.aad.brokerplugin/YOUR_CLIENT_ID
59+
```
60+
61+
If the redirect URL isn't configured, you will get a *broker_error* similar to *(pii). Status: Response_Status.Status_ApiContractViolation, Error code: 3399614473, Tag: 557973642*.
7062

7163
If configuration and instantiation was correct, once you run the application you should see the authentication broker kick in and allow the user to select the account they want to authenticate with.
7264

@@ -78,32 +70,30 @@ Worth noting that if you switch to using broker-based authentication, if the use
7870

7971
Depending on the authority specified when instantiating [`PublicClientApplication`](xref:msal.application.PublicClientApplication), the broker user interface may be different.
8072

81-
### `/consumers`
73+
### /consumers
8274

8375
Used for authenticating **only** with personal Microsoft accounts.
8476

8577
![WAM UI for consumers](../media/wam-consumers.png)
8678

87-
### `/common`
79+
### /common
8880

8981
Used for authenticating with personal Microsoft accounts as well as work and school accounts.
9082

9183
![WAM UI for personal and work accounts](../media/wam-common.png)
9284

93-
### `/organizations`
85+
### /organizations
9486

9587
Used for authenticating **only** with work and school accounts.
9688

9789
![WAM UI for work accounts only](../media/wam-organizations.png)
9890

99-
>[!NOTE]
100-
>If `login_hint` is provided but the account is not yet registered in WAM, the hint will be automatically filled in the "Email or phone" field.
91+
If *login_hint* is provided, but the account isn't yet registered in WAM, the hint will be automatically filled in the *Email or phone* field.
10192

102-
### `/TENANT_ID`
93+
### /TENANT_ID
10394

10495
Used for authenticating **only** with work and school accounts within the specified tenant.
10596

10697
![WAM UI for tenant-specific accounts](../media/wam-tenant-specific.png)
10798

108-
>[!NOTE]
109-
>If `login_hint` is provided but the account is not yet registered in WAM, the hint will be automatically filled in the "Email or phone" field.
99+
If *login_hint* is provided, but the account isn't yet registered in WAM, the hint will be automatically filled in the *Email or phone* field.

msal-python-conceptual/getting-started/client-applications.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ manager: CelesteDG
77
ms.service: msal
88
ms.subservice: msal-python
99
ms.topic: conceptual
10-
ms.date: 03/07/2024
10+
ms.date: 04/24/2025
1111
ms.author: shermanouko
1212
ms.reviewer: dmwendia, rayluo
1313
---
@@ -18,7 +18,7 @@ Microsoft Authentication Library (MSAL) Python supports two types of client appl
1818

1919
## Prerequisites
2020

21-
This article doesn't go deep into defining what a public and confidential client applications are. Visit the identity platform docs to learn more about [public and confidential client applications](/entra/identity-platform/msal-client-applications).
21+
Understand the basic concepts of [public and confidential client applications](/entra/identity-platform/msal-client-applications).
2222

2323
## Instantiate an application
2424

@@ -59,10 +59,10 @@ app = msal.ConfidentialClientApplication(
5959

6060
## Caching
6161

62-
When you instantiate a client application, there are two parameters you can use to define your caching preferences. These parameters are: `token_cache` and `http_cache`.
62+
When you instantiate a client application, there are two parameters you can use to define your caching preferences. These parameters are: *token_cache* and *http_cache*.
6363

64-
- `token_cache` sets the token cache used by the client application instance. By default, an in-memory cache is created and used. For more information, see [token caching in MSAL Python](../advanced/msal-python-token-cache-serialization.md).
65-
- `http_cache` is available in MSAL Python version 1.16+. This automatically caches some finite number of nontoken http responses, so that long-lived `PublicClientApplication` and `ConfidentialClientApplication` instances would be more performant and responsive in some situations. If the `http_cache` parameter isn't provided, MSAL uses an in-memory dict. If your app is a command-line app (CLI), you would want to persist your `http_cache` across different CLI runs. For more information, see the [reference guide](/python/api/msal/msal.application.clientapplication).
64+
- *token_cache* sets the token cache used by the client application instance. By default, an in-memory cache is created and used. For more information, see [token caching in MSAL Python](../advanced/msal-python-token-cache-serialization.md).
65+
- *http_cache* is available in MSAL Python version 1.16+. This automatically caches some finite number of nontoken http responses, so that long-lived *PublicClientApplication* and *ConfidentialClientApplication* instances would be more performant and responsive in some situations. If the *http_cache* parameter isn't provided, MSAL uses an in-memory dict. If your app is a command-line app (CLI), you would want to persist your *http_cache* across different CLI runs. For more information, see the [reference guide](/python/api/msal/msal.application.clientapplication).
6666

6767
## Next steps
6868

msal-python-conceptual/index.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ manager: CelesteDG
77
ms.service: msal
88
ms.subservice: msal-python
99
ms.topic: conceptual
10-
ms.date: 02/29/2024
10+
ms.date: 04/24/2025
1111
ms.author: shermanouko
1212
ms.reviewer: dmwendia, rayluo
1313
---
@@ -31,7 +31,7 @@ pip install msal
3131

3232
## Identity concepts
3333

34-
MSAL Python is part of the [Microsoft identity platform](/entra/identity-platform/v2-overview) ecosystem. It's important to familiarize yourself with the following concepts to effectively use MSAL Python to protect your applications and APIs:
34+
MSAL Python is part of the [Microsoft identity platform](/entra/identity-platform/v2-overview) ecosystem. Familiarize yourself with the following concepts to effectively use MSAL Python to protect your applications and APIs:
3535

3636
- [Identity and access management](/entra/fundamentals/identity-fundamental-concepts)
3737
- [Authentication and authorization](/entra/identity-platform/authentication-vs-authorization)
@@ -43,7 +43,7 @@ MSAL Python is part of the [Microsoft identity platform](/entra/identity-platfor
4343

4444
To use MSAL Python, register an application with the Microsoft identity platform. You'll need an Azure account with an active subscription. [Create a free account](https://signup.azure.com/) if you don't have one. You can register your app in a [customer tenant](/entra/external-id/customers/quickstart-tenant-setup) or [workforce tenant](/entra/identity-platform/scenario-web-app-sign-user-app-registration?tabs=python).
4545

46-
Applications can use MSAL Python to acquire tokens for accessing protected APIs. Different app types acquire tokens using different auth flows. The supported app types include desktop applications, web applications, web APIs, and applications running on devices that don't have a browser (such as IoT devices).
46+
Applications can use MSAL Python to acquire tokens for accessing protected APIs. Different app types acquire tokens using different auth flows. The supported app types include desktop applications, web applications, web APIs, and applications running on devices that don't have a browser (such as IoT devices).
4747

4848
In MSAL Python, applications are categorized as follows:
4949

@@ -90,7 +90,7 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
9090
result = app.acquire_token_silent(["User.Read"], account=chosen)
9191
```
9292

93-
1. If there's no suitable token in the cache or you chose to skip the previous step, send a request to Microsoft Entra ID to get a token. There are different methods based on your client type and scenario, but for the purposes of the example we're showing how to use [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive), which prompts the user to provide their credentials.
93+
1. If there's no suitable token in the cache or you chose to skip the previous step, send a request to Microsoft Entra ID to get a token. There are different methods based on your client type and scenario, but for the purposes of the example we're showing how to use [*acquire_token_interactive*](xref:msal.application.PublicClientApplication.acquire_token_interactive), which prompts the user to provide their credentials.
9494

9595
```python
9696
if not result:
@@ -104,13 +104,21 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
104104
print(result.get("correlation_id")) # You may need this when reporting a bug
105105
```
106106

107-
1. Save the code into a Python file locally, such as `msaltest.py`.
107+
1. Save the code into a Python file locally, such as *msaltest.py*.
108108
1. Run the code by executing `python .\msalpytest.py`. The following visual shows the sign-in experience for this example.
109109
110110
![Example of an app prompting the user to sign in with their account](./media/basic-pca-app-prompt.gif)
111111

112112
1. Once the authentication is completed and you closed the browser, you should be able to see the access token printed in the terminal.
113113

114+
## Best practices for a robust enterprise ready application
115+
116+
You can acquire a token for a protected Web API using MSAL Python. You also don't have to handle refreshing tokens yourself. However, to build robust, enterprise ready applications, you will need to do a bit more. For instance you'll want to:
117+
118+
- [Handle exceptions](/azure/active-directory/develop/msal-handling-exceptions?tabs=python), both when you acquire a token, but also when you call the protected Web API. In particular, if your application runs in a Microsoft Entra tenant where the tenant admins have set [Conditional Access](https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Conditional-Access-and-Claims-Challenges) policies to enforce Multiple Factor Authentication (MFA), you will need to handle a Claim challenge.
119+
120+
- You might want to enable [Logging](/azure/active-directory/develop/msal-logging?tabs=python) to troubleshoot your application and help your users, while respecting their privacy and being compliant with GDPR.
121+
114122
## Samples
115123

116124
There are several samples you can use to get started with MSAL Python.

0 commit comments

Comments
 (0)