Skip to content

Commit 27e421e

Browse files
committed
added images and script installation
1 parent 1a5119f commit 27e421e

7 files changed

Lines changed: 146 additions & 43 deletions

File tree

β€Žmsal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-broker-net.mdβ€Ž

Lines changed: 146 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,182 @@ title: # Add a title for the browser tab
77
description: # Add a meaningful description for search results
88
author: ploegert # GitHub alias
99
ms.author: jploegert # Microsoft alias
10-
ms.service: # Add the ms.service or ms.prod value
10+
ms.service: msal
1111
# ms.prod: # To use ms.prod, uncomment it and delete ms.service
12-
ms.topic: # Add the ms.topic value
13-
ms.date: 03/18/2025
12+
ms.topic: how-to
13+
ms.date: 05/08/2025
1414
---
1515

16-
# Using MSAL .NET with an Authentication Broker on Linux
17-
16+
# Use MSAL.NET with an authentication broker on Linux
1817

1918
> [!NOTE]
20-
> Microsoft Single Sign-on for Linux authentication broker support is introduced with `MSAL` version v4.69.1.
19+
> Microsoft single sign-on (SSO) for Linux authentication broker support is introduced with `Microsoft.Identity.Client` version v4.69.1.
2120
2221
Using an authentication broker on Linux enables you to simplify how your users authenticate with Microsoft Entra ID from your application, as well as take advantage of future functionality that protects Microsoft Entra ID refresh tokens from exfiltration and misuse.
2322

24-
Authentication brokers are **not** pre-installed on Linux but is bundled as a dependency of applications developed by Microsoft, such as [Company Portal](/mem/intune-service/user-help/enroll-device-linux). These applications are usually installed when a Linux computer is enrolled in a company's device fleet via an endpoint management solution like [Microsoft Intune](/mem/intune/fundamentals/what-is-intune). To learn more about Linux device set up with the Microsoft Identity Platform, refer to [Microsoft Enterprise SSO plug-in for Apple devices](/entra/identity-platform/apple-sso-plugin).
23+
An authentication broker is **not** pre-installed on standalone Linux but is bundled as a dependency of applications developed by Microsoft, such as [Company Portal](/mem/intune-service/user-help/enroll-device-linux). These applications are usually installed when a Linux computer is enrolled in a company's device fleet via an endpoint management solution like [Microsoft Intune](/mem/intune/fundamentals/what-is-intune). For [Windows Subsystem for Linux](/windows/wsl/about) (WSL) scenario, WAM (Windows Account Manager) is used as the broker. WAM does come pre-installed on the Windows system. To learn more about Linux device set up with the Microsoft Identity Platform, see [Microsoft Enterprise SSO plug-in for Apple devices](/entra/identity-platform/apple-sso-plugin).
2524

26-
## Usage
25+
## Prerequisites
2726

28-
To use the broker, you will need to install the broker-related packages in addition to the core MSAL from PyPI:
27+
To use the broker, you'll need to install a list of dependencies on the Linux platform:
2928

3029
```bash
31-
pip install msal[broker]>=1.31,<2
30+
libc++-dev
31+
libc++abi-dev
32+
libsecret-tools
33+
libwebkit2gtk-4.0
3234
```
3335

34-
>[!IMPORTANT]
35-
>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.31,<2"`.
3636

37-
Typically, on macOS your [public client](/entra/identity-platform/msal-client-applications) Python applications would [acquire tokens](../getting-started/acquiring-tokens.md) via the system browser. To use authentication brokers installed on a macOS system instead, you will need to pass an additional argument in the `PublicClientApplication` constructor - `enable_broker_on_mac`:
37+
## Create a console app on Linux platform
38+
39+
To use a broker on the Linux platform, set the `BrokerOptions` to `OperatingSystems.Linux`. Notice that we use the same option for both Windows Subsystem for Linux (WSL) and standalone Linux.
3840

39-
```python
41+
```csharp
4042
from msal import PublicClientApplication
41-
42-
app = PublicClientApplication(
43-
"CLIENT_ID",
44-
authority="https://login.microsoftonline.com/common",
45-
enable_broker_on_mac =True)
43+
44+
class Program
45+
{
46+
public static string ClientID = "your client id"; //msidentity-samples-testing tenant
47+
public static string[] Scopes = { "User.Read" };
48+
static void Main(string[] args)
49+
{
50+
Console.WriteLine("Hello World!");
51+
52+
var pcaBuilder = PublicClientApplicationBuilder.Create(ClientID)
53+
.WithAuthority("https://login.microsoftonline.com/common")
54+
.WithDefaultRedirectUri()
55+
.WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Linux){
56+
ListOperatingSystemAccounts = true,
57+
MsaPassthrough = true,
58+
Title = "MSAL WSL Test App"
59+
})
60+
.Build();
61+
62+
AcquireTokenInteractiveParameterBuilder atparamBuilder = pcaBuilder.AcquireTokenInteractive(Scopes);
63+
64+
AuthenticationResult authenticationResult = atparamBuilder.ExecuteAsync().GetAwaiter().GetResult();
65+
System.Console.WriteLine(authenticationResult.AccessToken);
66+
}
67+
}
4668
```
4769

48-
>[!IMPORTANT]
49-
>If you are writing a cross-platform application, you will also need to use `enable_broker_on_windows`, as outlined in the [Using MSAL Python with Web Account Manager](wam.md) article.
70+
## Sample App
71+
A sample application is available for developers who want to try the authentication broker on Linux. The application is located [in the MSAL.NET GitHub repository](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/tree/main/tests/devapps/WAM/NetWSLWam). The app has a dependency of `libx11-dev` package.
5072

51-
In addition to the constructor change, your application needs to support broker-specific redirect URIs. For _unsigned_ applications, the URI is:
73+
On Debian-based distributions, run `sudo apt install libx11-dev` to install the package. The `libx11` library is used to get the console window handle on Linux. Here's the sample code to use `libx11` to get the window handle:
5274

53-
```text
54-
msauth.com.msauth.unsignedapp://auth
55-
```
75+
```csharp
76+
using System;
77+
using System.Runtime.InteropServices;
5678

57-
For signed applications, the redirect URI should be:
79+
class X11Interop
80+
{
81+
[DllImport("libX11")]
82+
Β Β Β  public static extern IntPtr XOpenDisplay(IntPtr display);
83+
84+
Β Β Β  [DllImport("libX11")]
85+
Β Β Β  public static extern IntPtr XDefaultRootWindow(IntPtr display);
86+
87+
Β Β Β  public static void Main()
88+
Β Β Β  {
89+
Β Β Β Β Β Β Β  IntPtr display = XOpenDisplay(IntPtr.Zero);
90+
Β Β Β Β Β Β Β  if (display == IntPtr.Zero)
91+
Β Β Β Β Β Β Β  {
92+
Β Β Β Β Β Β Β Β Β Β Β  Console.WriteLine("Unable to open X display.");
93+
Β Β Β Β Β Β Β Β Β Β Β  return;
94+
Β Β Β Β Β Β Β  }
95+
96+
Β Β Β Β Β Β Β  IntPtr rootWindow = XDefaultRootWindow(display);
97+
Β Β Β Β Β Β Β  Console.WriteLine($"Root window handle: {rootWindow}");
98+
Β Β Β  }
99+
}
100+
```
58101

59-
```text
60-
msauth.BUNDLE_ID://auth
102+
To run the sample app, use the following command:
103+
```dotnetcli
104+
dotnet run --project tests\devapps\WAM\NetWSLWam\test.csproj
61105
```
62106

63-
If the redirect URIs are not correctly set in the app configuration within the Entra portal, you will receive error like this:
107+
## WSL Scenario
64108

65-
```text
66-
Error detected...
67-
tag=508170375
68-
context=AADSTS50011 Description: (pii), Domain: MSAIMSIDOAuthErrorDomain.Error was thrown in location: Broker
69-
errorCode=-51411
70-
status=Response_Status.Status_Unexpected
109+
### Update to the latest version of WSL
110+
111+
Enure you have updated to the latest WSL release. The WAM Account Control dialog is supported in WSL versions 2.4.13 and above. Using the broker isn't possible with earlier versions.
112+
113+
```powershell
114+
115+
#to start from scratch:
116+
wsl --unregister Ubuntu-22.04
117+
118+
#To check what distros are available:
119+
wsl.exe --list --online
120+
121+
wsl.exe --install Ubuntu-22.04
122+
123+
# To check the WSL version, use the following command:
124+
wsl --version
125+
126+
# To update WSL, run the following command from Windows Terminal:
127+
wsl --update
71128
```
72129

73-
Once configured, you can call `acquire_token_interactive` to acquire a token.
130+
```bash
131+
sudo apt update -y
132+
133+
# Setup .NET:
134+
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
135+
chmod +x ./dotnet-install.sh
136+
./dotnet-install.sh --version latest
137+
export DOTNET_ROOT=~/.dotnet
138+
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools
139+
140+
dotnet --info
141+
sudo apt update
142+
sudo apt upgrade -y
143+
sudo apt install seahorse libsecret-1-0 libx11-dev libc++-dev libc++abi-dev libsecret-tools libwebkit2gtk-4.0-dev -y
144+
sudo apt install libc6 libgcc1 libgssapi-krb5-2 libicu70 libssl3 libstdc++6 zlib1g -y
145+
146+
sudo seahorse
147+
# update the keychain
148+
149+
# Reboot
150+
wsl.exe --shutdown
151+
export DOTNET_ROOT=~/.dotnet
152+
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools
153+
154+
cd /mnt/c/git/microsoft-authentication-library-for-dotnet/tests/devapps/WAM/NetWSLWam/
155+
dotnet build
156+
donet run
157+
158+
#Or do this
159+
cd /mnt/c/git/microsoft-authentication-library-for-dotnet
160+
dotnet run --project tests/devapps/WAM/NetWSLWam/test.csproj
74161

75-
```python
76-
result = app.acquire_token_interactive(["User.ReadBasic.All"],
77-
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
78162
```
79163

80-
>[!NOTE]
81-
>The `parent_window_handle` parameter is required even though on macOS it is not used. For GUI applications, the login prompt location will be determined ad-hoc and currently cannot be bound to a specific window. In a future update, this parameter will be used to determine the _actual_ parent window.
82164

83-
## Token caching
165+
### Set up Keyring in WSL
166+
MSAL uses `libsecret` on Linux. It is required to communicate with the `keyring` daemon. Users can use [Seahorse](https://wiki.gnome.org/Apps/Seahorse/) (a GNOME application for managing encryption keys and passwords) to manage the `keyring` contents through a Graphical User Interface (GUI).
167+
168+
On Debian-based distributions, you can install the package by running `sudo apt install seahorse` and then following these instructions:
169+
170+
1. Run `seahorse` in the terminal.
171+
172+
![WSL1](../../media/wam/wsl1.png)
173+
174+
2. In the top left corner, click **+** and create **Password** keyring.
175+
176+
![WSL2](../../media/wam/wsl2.png)
177+
178+
3. Create a keyring named 'login'
179+
180+
![WSL3](../../media/wam/wsl3.png)
181+
182+
4. Set the password on the next dialog.
183+
![WSL4](../../media/wam/wsl4.png)
184+
185+
5. Run `wsl.exe --shutdown` from your Windows Terminal.
186+
187+
6. Start a new WSL session and run the sample. You should be asked for the keyring password.
84188

85-
The authentication broker handles refresh and access token caching. You do not need to set up custom caching.
28 KB
Loading
28.3 KB
Loading
25.6 KB
Loading
16.6 KB
Loading
16.5 KB
Loading
18.2 KB
Loading

0 commit comments

Comments
Β (0)