Skip to content

Add doc about linux broker on dotnet scenario #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c631925
add doc about linux broker on dotnet scenario
xinyuxu1026 May 5, 2025
ffa809e
add more info
xinyuxu1026 May 6, 2025
83ccb15
update author
xinyuxu1026 May 6, 2025
2be64ed
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
3c184e4
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
e30b448
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
182fe07
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
e425098
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
d0b2baf
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
2099411
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
2fca0c8
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
17e2eb3
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
ae7f038
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
269356c
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
82e796f
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
5907d42
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
2e29b6e
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
c973e74
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
871cfdd
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
82d1694
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
localden May 6, 2025
b65ae78
Address comments
xinyuxu1026 May 6, 2025
528ab4a
Update linux-broker-net.md
localden May 6, 2025
1cd63df
add sample code
xinyuxu1026 May 6, 2025
e07e91f
Merge branch 'linux-dotnet-with-broker' of https://github.com/xinyuxu…
xinyuxu1026 May 6, 2025
d83a3a4
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
384f135
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
837040d
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
4a28d7e
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
fe81a0e
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
bac9b31
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
d1bee1e
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
904d17c
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
ee47017
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 6, 2025
23e7903
address comments
xinyuxu1026 May 6, 2025
934edbc
add screenshot and .net install
xinyuxu1026 May 7, 2025
ddffeae
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 8, 2025
749cb8d
Update msal-dotnet-articles/acquiring-tokens/desktop-mobile/linux-bro…
xinyuxu1026 May 8, 2025
65ce187
add more screenshot
xinyuxu1026 May 8, 2025
b5c37a3
more update
xinyuxu1026 May 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
# Required metadata
# For more information, see https://review.learn.microsoft.com/en-us/help/platform/learn-editor-add-metadata?branch=main
# For valid values of ms.service, ms.prod, and ms.topic, see https://review.learn.microsoft.com/en-us/help/platform/metadata-taxonomies?branch=main

title: Use MSAL.NET with an authentication broker on Linux
description: "Learn how to use MSAL.NET with a broker on the Linux platform."
author: xinyuxu1026 # GitHub alias
ms.author: xinyuxu # Microsoft alias
ms.service: msal
# ms.prod: # To use ms.prod, uncomment it and delete ms.service
ms.topic: how-to
ms.date: 05/05/2025
---

# Use MSAL.NET with an authentication broker on Linux


> [!NOTE]
> Microsoft single sign-on (SSO) for Linux authentication broker support is introduced with `Microsoft.Identity.Client` version v4.69.1.
Comment on lines +19 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this note should be removed. Instead, add the following .NET CLI commands to the Prereqs section below with minimum required versions:

dotnet add package Microsoft.Identity.Client
dotnet add package Microsoft.Identity.Client.Broker


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.

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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To learn more about Linux device set up with the Microsoft Identity Platform, see Microsoft Enterprise SSO plug-in for Apple devices.

Could you clarify how this doc for Apple devices is helpful for Linux machines?


## Prerequisites

MSAL.NET project requires .NET version greater or equal than the version specified in [global.json](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/global.json) file, we recommend register the Microsoft package repository and install .NET, please follow the instructions here: [Install .NET on Ubuntu](/dotnet/core/install/linux-ubuntu-decision#register-the-microsoft-package-repository). And please make sure you have all the [.NET required dependencies](/dotnet/core/install/linux-ubuntu-decision#dependencies) installed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear to me why I should care about what the MSAL.NET project requires.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the part in which it recommends registering the Microsoft Package Repo and install .NET from it, cause Ubuntu has its own feed and it took me a while to install net8.0.4.


To use the broker, you'll need to install a list of dependencies on the Linux platform:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also note/link to the user on the fact that dotnet is required to be installed for the sample. Perhaps we can consolidate a "prep step" that calls out below:

sudo apt install seahorse libx11-dev

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Justin, I added the .net installation part and the version requirement, could you help take a look again? Thanks


```bash
libc++-dev
libc++abi-dev
libsecret-tools
libwebkit2gtk-4.0-dev
```

On Debian-based distributions, run this command to install dependencies:
```bash
sudo apt install libc++-dev libc++abi-dev libsecret-tools libwebkit2gtk-4.0-dev - y
```

## Create a console app on Linux platform

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
To use a broker on Linux, pass `BrokerOptions.OperatingSystems.Linux` to the `BrokerOptions` instance. Notice that we use the same option for both Windows Subsystem for Linux (WSL) and standalone Linux.


```csharp
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Broker;

class Program
{
public static string ClientID = "your client id"; //msidentity-samples-testing tenant
public static string[] Scopes = { "User.Read" };
static void Main(string[] args)
{
IPublicClientApplication pcaBuilder = PublicClientApplicationBuilder.Create(ClientID)
.WithAuthority("https://login.microsoftonline.com/common")
.WithDefaultRedirectUri()
.WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Linux)
{
ListOperatingSystemAccounts = true,
MsaPassthrough = true,
Title = "MSAL WSL Test App"
})
.Build();

AcquireTokenInteractiveParameterBuilder atParamBuilder = pcaBuilder.AcquireTokenInteractive(Scopes);

AuthenticationResult authenticationResult = atParamBuilder.ExecuteAsync().GetAwaiter().GetResult();
Console.WriteLine(authenticationResult.AccessToken);
}
}
```

## Sample App
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.

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:

```csharp
using System;
using System.Runtime.InteropServices;

class X11Interop
{
[DllImport("libX11")]
    public static extern IntPtr XOpenDisplay(IntPtr display);

    [DllImport("libX11")]
    public static extern IntPtr XDefaultRootWindow(IntPtr display);

    public static void Main()
    {
        IntPtr display = XOpenDisplay(IntPtr.Zero);
        if (display == IntPtr.Zero)
        {
            Console.WriteLine("Unable to open X display.");
            return;
        }

        IntPtr rootWindow = XDefaultRootWindow(display);
        Console.WriteLine($"Root window handle: {rootWindow}");
    }
}
```

We recommend play with the sample app using your client id. To run the sample app, use the following command:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We recommend play with the sample app using your client id. To run the sample app, use the following command:
We recommend playing with the sample app using your client ID. To run the sample app, use the following command:

```dotnetcli
dotnet run --project tests\devapps\WAM\NetWSLWam\test.csproj
```

## WSL Scenario

### Update to the latest version of WSL

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. To check the WSL version, use the following command:

```powershell
wsl --version
```

To update WSL, run the following command from Windows Terminal:

```powershell
wsl --update
```

### Set up Keyring in WSL
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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be helpful to add more details on how Keyring plays a role with the library and WAM. Keyring might be confused with WAM in some way.


On Debian-based distributions, you can install the package by running `sudo apt install seahorse` and then following these instructions:

1. Run `seahorse` in the terminal.
2. In the top left corner, click **+** and create **Password** keyring.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. In the top left corner, click **+** and create **Password** keyring.
2. In the top left corner, select **+** and create **Password** keyring.


![Screenshot that shows the seahorse interface to create Password keyring.](../../media/msal-net-linux/password-keyring-seahorse.png)

3. Create a keyring named 'login' and set the password.

![Screenshot that creates keyring.](../../media/msal-net-linux/create-keyring-named-login.png)

![Screenshot that enters password.](../../media/msal-net-linux/choose-password.png)

![Screenshot that closes the keyring window.](../../media/msal-net-linux/close-keyring-window.png)

4. Run `wsl.exe --shutdown` from your Windows Terminal.
5. Start a new WSL session and run the sample. You should be asked for the keyring password.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.