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: "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."
4
+
---
5
+
6
+
# Using MSAL Python with Web Account Manager
7
+
8
+
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).
9
+
10
+
>[!NOTE]
11
+
>WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
12
+
13
+
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.
14
+
15
+
## Usage
16
+
17
+
To use the broker, you will need to install the broker-related packages in addition to the core MSAL from PyPI:
18
+
19
+
```bash
20
+
pip install msal[broker]>=1.20,<2
21
+
```
22
+
23
+
>[!IMPORTANT]
24
+
>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"`.
25
+
26
+
Next, you will need to instantiate a new [`PublicClientApplication`](xref:msal.application.PublicClientApplication) and set `allow_broker` to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window.
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`:
38
+
39
+
```python
40
+
result = app.acquire_token_interactive(["User.ReadBasic.All"],
41
+
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
42
+
```
43
+
44
+
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.
45
+
46
+
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.
47
+
48
+
Before executing your application, make sure that you configure the redirect URL for the desktop app:
49
+
50
+
>[!IMPORTANT]
51
+
>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`.
58
+
59
+
If configuration and instantiation was correct, once you run the application you should see the authentication broker kick in and allow the user to selectthe account they want to authenticate with.
60
+
61
+

62
+
63
+
Worth noting that if you switch to using broker-based authentication, if the user was previously logged in and the signed-in state is still valid, calling [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) will still result in a silent attempt to acquire a token, and only prompt when necessary. If you prefer to always prompt, you can use this optional parameter `prompt="select_account"`.
64
+
65
+
## Broker experience differences
66
+
67
+
Depending on the authority specified when instantiating [`PublicClientApplication`](xref:msal.application.PublicClientApplication), the broker user interface may be different.
68
+
69
+
### `/consumers`
70
+
71
+
Used for authenticating **only** with personal Microsoft accounts.
72
+
73
+

74
+
75
+
### `/common`
76
+
77
+
Used for authenticating with personal Microsoft accounts as well as work and school accounts.
78
+
79
+

80
+
81
+
### `/organizations`
82
+
83
+
Used for authenticating **only** with work and school accounts.
84
+
85
+

86
+
87
+
>[!NOTE]
88
+
>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.
89
+
90
+
### `/TENANT_ID`
91
+
92
+
Used for authenticating **only** with work and school accounts within the specified tenant.
93
+
94
+

95
+
96
+
>[!NOTE]
97
+
>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.
Copy file name to clipboardExpand all lines: msal-python-conceptual/getting-started/acquiring-tokens.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Before acquiring tokens with MSAL Python, make sure to instantiate a [client app
13
13
14
14
## Token acquisition methods
15
15
16
-
The approach to acquiring a token is different depending on the application type - public client applications (desktop and mobile) or confidential client application (web app, web API, or a daemon application like a Windows service). Each of the individual approaches is described below.
16
+
The approach to acquiring a token is different depending on the application type - public client applications (desktop and mobile) or confidential client application (web app, web API, or a daemon application like a Windows service). Each of the individual approaches is described below.
Copy file name to clipboardExpand all lines: msal-python-conceptual/getting-started/client-applications.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ Before instantiating your app with MSAL Python:
17
17
- The identity provider URL (the instance) and the sign-in audience for your application. These two parameters are collectively known as the **authority**.
18
18
- If necessary, the tenant identifier (also a GUID) in case you are writing a line of business application scoped to just your organization (also known as a single-tenant application).
19
19
- If you are building a confidential client app, you will need to create an application secret in the form of a string or certificate.
20
-
- For web applications, you'll have also set the redirect URL that Azure AD will use to return the code.
20
+
- For web applications, you'll have also set the redirect URL that Azure AD will use to return the code. For desktop applications you will need to add `http://localhost` if you're not relying on authentication brokers.
Copy file name to clipboardExpand all lines: msal-python-conceptual/index.md
+44-17Lines changed: 44 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,28 +22,39 @@ pip install msal
22
22
23
23
## Setting up
24
24
25
-
Before using MSAL Python, make sure to [register your application](/azure/active-directory/develop/quickstart-v2-register-an-app) with the Microsoft identity platform.
25
+
Before using MSAL Python, make sure to [register your application](/azure/active-directory/develop/quickstart-v2-register-an-app) with the Microsoft identity platform. You will need to take note of your **client ID** and **tenant ID**.
26
26
27
-
## Usage
27
+
>[!IMPORTANT]
28
+
>When registering the application, make sure that you set up **redirect URLs** within the **Authentication** blade. Redirect URLs vary depending on the target platform.
29
+
>
30
+
>
31
+
>
32
+
>For desktop and mobile applications, make sure you add `http://localhost` as redirect URL if you do not rely on authentication brokers.
33
+
34
+
## Basic usage
28
35
29
36
Acquiring tokens with MSAL Python follows a three-step pattern. There will be some variations for different flows. If you would like to see them in action, download our [samples](https://github.com/AzureAD/microsoft-authentication-library-for-python/tree/dev/sample).
30
37
31
38
1. MSAL relies on a clean separation between [public client and confidential client applications](https://tools.ietf.org/html/rfc6749#section-2.1). Therefore, create either a [`PublicClientApplication`](xref:msal.application.PublicClientApplication) or a [`ConfidentialClientApplication`](xref:msal.application.ConfidentialClientApplication) instance and reuse it during the lifecycle of your application. For example, for a public client application, the initalization code might look like this:
result =None# It is just an initial value. Please follow instructions below.
44
-
```
48
+
>[!NOTE]
49
+
>The authority isset to `/common` to allow sign ins with both organizaiton and personal Microsoft accounts. You can change it to `/organizations` to only allow sign ins with work and school accounts, `/consumers` to only allow personal Microsoft accounts, orwith`/YOUR_TENANT_ID` to only allow sign ins from work and school accounts associated with your tenant.
50
+
51
+
Instantiate a variable to hold the authentication result:
52
+
53
+
```python
54
+
result =None# It is just an initial value. Please follow instructions below.
55
+
```
45
56
46
-
2. The API model in MSAL provides you explicit control on how to utilize the token cache. While the caching part is technically optional, we highly recommend you to use it in your application. Using the cache you can ensure that you're not making any extra API calls and handle the token refresh automatically.
57
+
2. Try and obtain the tokens from the cache first. The API model inMSAL provides you explicit control on how to utilize the token cache. While the caching part is technically optional, we highly recommend you to use it in your application. Using the cache you can ensure that you're not making any extra API calls and handle the token refresh automatically.
47
58
48
59
```python
49
60
# We now check the cache to see
@@ -57,15 +68,15 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
57
68
# Assuming the end user chose this one
58
69
chosen = accounts[0]
59
70
# Now let's try to find a token in cache for this account
60
-
result = app.acquire_token_silent(["your_scope"], account=chosen)
71
+
result = app.acquire_token_silent(["User.Read"], account=chosen)
61
72
```
62
73
63
-
3. If there is no suitable token in the cache or you've chosen to skip the previous step, send a request to Azure AD to get a token. There are different methods based on your client type and scenario. A sample flow can look like this:
74
+
3. If there is no suitable token in the cache or you've chosen to skip the previous step, send a request to Azure AD 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 will prompt the user to provide their credentials.
64
75
65
76
```python
66
77
ifnot result:
67
78
# So no suitable token exists in cache. Let's get a new one from Azure AD.
68
-
result = app.acquire_token_by_one_of_the_actual_method(..., scopes=["User.Read"])
79
+
result = app.acquire_token_interactive(scopes=["User.Read"])
69
80
if"access_token"in result:
70
81
print(result["access_token"]) # Yay!
71
82
else:
@@ -74,6 +85,18 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
74
85
print(result.get("correlation_id")) # You may need this when reporting a bug
75
86
```
76
87
88
+
4. Save the code into a Python file locally, such as `msaltest.py`.
89
+
5. Run the code by executing `python .\msalpytest.py`.
90
+
91
+
>[!NOTE]
92
+
>You can also download runnable samples from the [library repository](https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/1.22.0/sample/interactive_sample.py).
93
+
94
+
If the application was configured correctly, you should see a web browser window appear asking the user to sign in.
95
+
96
+

97
+
98
+
Once the authentication is completed and you closed the browser, you should be able to see the access token printed in the terminal.
99
+
77
100
## Usage scenarios
78
101
79
102
MSAL Python can be used by applications to acquire tokens to access protected APIs. Tokens can be acquired by different **application types**: desktop applications, web applications, web APIs, and applications running on devices that don't have a browser (such as IoT devices). In MSAL Python, applications are categorized as follows:
@@ -95,4 +118,8 @@ Key scenarios supported by MSAL Python:
95
118
-[Desktop/service daemon application calling Web API without a user](/azure/active-directory/develop/scenario-daemon-overview)
96
119
-[Application without a browser, or IOT application calling an API in the name of the user](/azure/active-directory/develop/scenario-desktop-acquire-token?tabs=python#command-line-tool-without-web-browser)
97
120
98
-
Can't find the scenario you are looking for? Check out the [supported scenarios and platforms](/azure/active-directory/develop/authentication-flows-app-scenarios#scenarios-and-supported-platforms-and-languages) across MSAL libraries.
121
+
Can't find the scenario you are looking for? Check out the [supported scenarios and platforms](/azure/active-directory/develop/authentication-flows-app-scenarios#scenarios-and-supported-platforms-and-languages) across MSAL libraries.
122
+
123
+
## Releases
124
+
125
+
Refer to [MSAL Python releases on GitHub](https://github.com/AzureAD/microsoft-authentication-library-for-python/releases).
0 commit comments