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: msal-python-conceptual/advanced/wam.md
+21-31Lines changed: 21 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,19 +7,16 @@ manager: CelesteDG
7
7
ms.service: msal
8
8
ms.subservice: msal-python
9
9
ms.topic: conceptual
10
-
ms.date: 02/07/2024
10
+
ms.date: 04/24/2025
11
11
ms.author: dmwendia
12
12
ms.reviewer: shermanouko, rayluo
13
13
---
14
14
15
15
# Using MSAL Python with Web Account Manager
16
16
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.
18
18
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.
23
20
24
21
## Usage
25
22
@@ -29,13 +26,9 @@ To use the broker, you will need to install the broker-related packages in addit
29
26
pip install msal[broker]>=1.20,<2
30
27
```
31
28
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"*.
36
30
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.
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*:
50
43
51
44
```python
52
45
result = app.acquire_token_interactive(["User.ReadBasic.All"],
53
46
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
54
47
```
55
48
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.
57
50
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.
59
52
60
53
Before executing your application, make sure that you configure the redirect URL for the desktop app:
61
54
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:
>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:
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*.
70
62
71
63
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.
72
64
@@ -78,32 +70,30 @@ Worth noting that if you switch to using broker-based authentication, if the use
78
70
79
71
Depending on the authority specified when instantiating [`PublicClientApplication`](xref:msal.application.PublicClientApplication), the broker user interface may be different.
80
72
81
-
### `/consumers`
73
+
### /consumers
82
74
83
75
Used for authenticating **only** with personal Microsoft accounts.
84
76
85
77

86
78
87
-
### `/common`
79
+
### /common
88
80
89
81
Used for authenticating with personal Microsoft accounts as well as work and school accounts.
90
82
91
83

92
84
93
-
### `/organizations`
85
+
### /organizations
94
86
95
87
Used for authenticating **only** with work and school accounts.
96
88
97
89

98
90
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.
101
92
102
-
### `/TENANT_ID`
93
+
### /TENANT_ID
103
94
104
95
Used for authenticating **only** with work and school accounts within the specified tenant.
105
96
106
97

107
98
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.
Copy file name to clipboardExpand all lines: msal-python-conceptual/getting-started/client-applications.md
+5-5Lines changed: 5 additions & 5 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: msal
8
8
ms.subservice: msal-python
9
9
ms.topic: conceptual
10
-
ms.date: 03/07/2024
10
+
ms.date: 04/24/2025
11
11
ms.author: shermanouko
12
12
ms.reviewer: dmwendia, rayluo
13
13
---
@@ -18,7 +18,7 @@ Microsoft Authentication Library (MSAL) Python supports two types of client appl
18
18
19
19
## Prerequisites
20
20
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).
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*.
63
63
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).
Copy file name to clipboardExpand all lines: msal-python-conceptual/index.md
+13-5Lines changed: 13 additions & 5 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: msal
8
8
ms.subservice: msal-python
9
9
ms.topic: conceptual
10
-
ms.date: 02/29/2024
10
+
ms.date: 04/24/2025
11
11
ms.author: shermanouko
12
12
ms.reviewer: dmwendia, rayluo
13
13
---
@@ -31,7 +31,7 @@ pip install msal
31
31
32
32
## Identity concepts
33
33
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:
35
35
36
36
-[Identity and access management](/entra/fundamentals/identity-fundamental-concepts)
37
37
-[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
43
43
44
44
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).
45
45
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).
47
47
48
48
In MSAL Python, applications are categorized as follows:
49
49
@@ -90,7 +90,7 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
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.
94
94
95
95
```python
96
96
ifnot result:
@@ -104,13 +104,21 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
104
104
print(result.get("correlation_id")) # You may need this when reporting a bug
105
105
```
106
106
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*.
108
108
1. Run the code by executing `python .\msalpytest.py`. The following visual shows the sign-in experience for this example.
109
109
110
110

111
111
112
112
1. Once the authentication is completed and you closed the browser, you should be able to see the access token printed in the terminal.
113
113
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 andhelp your users, while respecting their privacy and being compliant withGDPR.
121
+
114
122
## Samples
115
123
116
124
There are several samples you can use to get started withMSAL Python.
0 commit comments