-
Notifications
You must be signed in to change notification settings - Fork 6k
Extracted broker auth into new doc #48046
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
2d2853f
Extracted broker auth into new doc
alexwolfmsft 162869a
fix metadata
alexwolfmsft 835f844
fix toc
alexwolfmsft 5d910e0
Apply suggestions from code review
alexwolfmsft 8fc22b8
Apply suggestions from code review
alexwolfmsft df06fe4
Apply suggestions from code review
alexwolfmsft 04ba363
Apply suggestions from code review
alexwolfmsft fdc226f
temp
alexwolfmsft f85473b
temp
alexwolfmsft 881a038
add additional OS
alexwolfmsft 2761f6c
fix zone pivots
alexwolfmsft ca6362f
Maui fixes
alexwolfmsft 61f5d96
fix code blocks
alexwolfmsft 4759332
refactor intro
alexwolfmsft d410988
fixes
alexwolfmsft ede3fa1
fixes
alexwolfmsft 5892f9d
fix code lines
alexwolfmsft 6e787bd
fixes
alexwolfmsft 893b0bc
changes
alexwolfmsft ec7ac00
fixes
alexwolfmsft 08ef06a
fixes
alexwolfmsft d7e79fc
fixes
alexwolfmsft e031539
fixes
alexwolfmsft dcab7fe
Apply suggestions from code review
alexwolfmsft 9b58686
Apply suggestions from code review
alexwolfmsft 7d3fd3a
PR Fixes
alexwolfmsft c836ded
update
alexwolfmsft 8ff30d2
code update
alexwolfmsft e0d208e
fix secrets
alexwolfmsft 9a606ac
remove secret
alexwolfmsft 420e5c9
fix compile
alexwolfmsft 8f95647
Apply suggestions from code review
alexwolfmsft b530aec
zone pivot change
alexwolfmsft d07511e
Apply suggestions from code review
alexwolfmsft c26c551
Apply suggestions from code review
alexwolfmsft 8896a4f
Merge
alexwolfmsft 5e09236
Merge branch 'new-broker-auth-doc' of https://github.com/alexwolfmsft…
alexwolfmsft 8fdcde2
fixes
alexwolfmsft 4db4afe
Apply suggestions from code review
alexwolfmsft f2af6cd
Add macOS screenshot
scottaddie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
docs/azure/sdk/authentication/local-development-broker.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
title: Authenticate .NET apps to Azure using brokered authentication. | ||
description: Learn how to authenticate your app to Azure services when using the Azure SDK for .NET during local development using brokered authentication. | ||
ms.topic: how-to | ||
ms.custom: devx-track-dotnet, engagement-fy23, devx-track-azurecli | ||
ms.date: 08/20/2025 | ||
zone_pivot_groups: operating-systems-set-one | ||
--- | ||
|
||
# Authenticate .NET apps to Azure services during local development using interactive brokered authentication | ||
|
||
Interactive brokered authentication collects user credentials using the system authentication broker to authenticate an app with <xref:Azure.Identity.InteractiveBrowserCredential>. A system authentication broker is an app running on a user's machine that manages the authentication handshakes and token maintenance for all connected accounts. | ||
|
||
Interactive brokered authentication offers the following benefits: | ||
|
||
- **Enables Single Sign-On (SSO):** Enables apps to simplify how users authenticate with Microsoft Entra ID and protects Microsoft Entra ID refresh tokens from exfiltration and misuse. | ||
- **Enhanced security.** Many security enhancements are delivered with the broker, without needing to update the app logic. | ||
- **Feature support.** With the help of the broker developers can access rich OS and service capabilities. | ||
- **System integration.** Applications that use the broker plug-and-play with the built-in account picker, allowing the user to quickly pick an existing account instead of reentering the same credentials over and over. | ||
- **Token Protection.** Ensures that the refresh tokens are device bound and enables apps to acquire device bound access tokens. See [Token Protection](/azure/active-directory/conditional-access/concept-token-protection). | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
:::zone target="docs" pivot="os-windows" | ||
|
||
Windows provides an authentication broker called [Web Account Manager (WAM)](/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam). Wam enables identity providers such as Microsoft Entra ID to natively plug into the OS and provide the service to other apps for a more secure login process. Brokered authentication enables the app for all operations allowed by the interactive login credentials. | ||
|
||
Personal Microsoft accounts and work or school accounts are supported. If a supported version of Windows is used, the default browser-based UI is replaced with a smoother authentication experience, similar to Windows built-in apps. | ||
|
||
:::zone-end | ||
|
||
:::zone target="docs" pivot="os-macos" | ||
|
||
Authentication brokers aren't preinstalled on macOS, but they are available through apps developed by Microsoft, such as Company Portal. These apps are installed when a macOS device is enrolled in a company device fleet via a solution like Microsoft Intune. To learn more about Apple device set up with the Microsoft Identity Platform, refer to [Microsoft Enterprise SSO plug-in for Apple devices](/entra/identity-platform/apple-sso-plugin). | ||
|
||
:::zone-end | ||
|
||
:::zone target="docs" pivot="os-linux" | ||
|
||
The Linux operating system uses [Microsoft single sign-on for Linux](/entra/identity/devices/sso-linux) as its authentication broker. | ||
|
||
> [!NOTE] | ||
> Microsoft SSO for Linux authentication broker support is introduced with `Azure.Identity.Broker` version v1.3.0. | ||
|
||
:::zone-end | ||
|
||
## Configure the app for brokered authentication | ||
|
||
Complete the following steps to enable the application to authenticate through the broker flow: | ||
|
||
1. In the [Azure portal](https://portal.azure.com), navigate to **Microsoft Entra ID** and select **App registrations** on the left-hand menu. | ||
1. Select the registration for your app, then select **Authentication**. | ||
1. Add the redirect URI to your app registration via a platform configuration: | ||
1. Under **Platform configurations**, select **+ Add a platform**. | ||
1. Under **Configure platforms**, select the tile for your application type (platform) to configure its settings, such as **mobile and desktop applications**. | ||
1. In **Custom redirect URIs**, enter the following redirect URI for your platform: | ||
|
||
| Platform | Redirect URI | | ||
|-------------|-----------------------------------------------------------------------------------------------------------------------| | ||
| Windows 10+ | `ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id` | | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| macOS | `msauth.com.msauth.unsignedapp://auth` for unsigned apps `msauth.BUNDLE_ID://auth` for signed apps | | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| WSL | `ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id` | | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Linux | `https://login.microsoftonline.com/common/oauth2/nativeclient` | | ||
alexwolfmsft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `{your_client_id}` or `{BUNDLE_ID}` placeholder must be replaced with the **Application (client) ID** listed on the **Overview** pane of the app registration. | ||
|
||
1. Select **Configure**. | ||
|
||
To learn more, see [Add a redirect URI to an app registration](/entra/identity-platform/quickstart-register-app#add-a-redirect-uri). | ||
|
||
1. Back on the **Authentication** pane, under **Advanced settings**, select **Yes** for **Allow public client flows**. | ||
1. Select **Save** to apply the changes. | ||
1. To authorize the application for specific resources, navigate to the resource in question, select **API Permissions**, and enable **Microsoft Graph** and other resources you want to access. | ||
|
||
> [!IMPORTANT] | ||
> You must also be the admin of your tenant to grant consent to your application when you sign in for the first time. | ||
|
||
### Assign roles | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
For your app code to run successfully with brokered auth, you need grant your user account permissions through [Azure role-based access control (RBAC)](/azure/role-based-access-control/overview). Your user account must be [assigned an appropriate role](/dotnet/azure/sdk/authentication/local-development-dev-accounts) on the corresponding Azure service. For example: | ||
|
||
- **Azure Blob Storage**: Assign a role such as **Storage Account Data Contributor**. | ||
- **Azure Key Vault**: Assign a role such as **Key Vault Secrets Officer**. | ||
|
||
If an app is specified, it must have API permissions set for **user_impersonation Access Azure Storage** (step 6 in the previous section). This API permission allows the app to access Azure storage on behalf of the signed-in user after consent is granted during sign-in. | ||
|
||
## Implement the code | ||
|
||
Complete the following steps to use <xref:Azure.Identity.InteractiveBrowserCredential> in a MAUI app to authenticate with the [`SecretClient`](/dotnet/api/azure.security.keyvault.secrets.secretclient): | ||
|
||
1. Install the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) and [Azure.Identity.Broker](https://www.nuget.org/packages/Azure.Identity.Broker) packages. | ||
|
||
> [!NOTE] | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
> macOS and Linux support exists in `Azure.Identity.Broker` versions 1.3.0 and later. | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
1. Get a reference to the parent window on top of which the account picker dialog should appear. | ||
1. Create an instance of <xref:Azure.Identity.InteractiveBrowserCredential> that accepts an instance of <xref:Azure.Identity.Broker.InteractiveBrowserCredentialBrokerOptions>. | ||
|
||
:::zone target="docs" pivot="os-windows" | ||
|
||
:::code language="csharp" source="../snippets/authentication/brokered/maui-app/MainPage.xaml.cs" highlight="37-49" ::: | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
The following screenshot shows the user sign-in experience: | ||
|
||
:::image type="content" source="../media/web-account-manager-sign-in-account-picker.png" alt-text="A screenshot that shows the sign-in experience when using a broker-enabled InteractiveBrowserCredential instance to authenticate a user." ::: | ||
|
||
:::zone-end | ||
|
||
:::zone target="docs" pivot="os-macos" | ||
|
||
:::code language="csharp" source="../snippets/authentication/brokered/maui-app/MainPage.xaml.cs" highlight="53-65" ::: | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
:::zone-end | ||
|
||
:::zone target="docs" pivot="os-linux" | ||
|
||
:::code language="csharp" source="../snippets/authentication/brokered/console-app/Program.cs" highlight="22-28" ::: | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
:::zone-end | ||
|
||
### Authenticate the default system account | ||
|
||
`InteractiveBrowserCredential` also supports a silent sign-in process that automatically uses a default account so the user doesn't have to repeatedly select it. Once you opt-in to this behavior, the credential attempts to sign-in by asking the underlying Microsoft Authentication Library (MSAL) to perform the sign-in for the default system account. If the sign-in fails, the credential falls back to displaying the account picker dialog, from which the user can select the appropriate account. | ||
|
||
The previous example shows how to enable sign-in with the default system account: | ||
|
||
:::code language="csharp" source="../snippets/authentication/brokered/console-app/Program.cs" range="22-28" highlight="3" ::: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
docs/azure/sdk/snippets/authentication/brokered/console-app/BrokeredConsole.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ProjectUISubcaption>Linux</ProjectUISubcaption> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" /> | ||
<PackageReference Include="Azure.Identity.Broker" /> | ||
<PackageReference Include="Azure.Security.KeyVault.Secrets" /> | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
</ItemGroup> | ||
</Project> |
53 changes: 53 additions & 0 deletions
53
docs/azure/sdk/snippets/authentication/brokered/console-app/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Azure; | ||
using Azure.Identity; | ||
using Azure.Identity.Broker; | ||
using Azure.Security.KeyVault.Secrets; | ||
|
||
/// <summary> | ||
alexwolfmsft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Get the handle of the console window for Linux | ||
/// </summary> | ||
[DllImport("libX11")] | ||
static extern IntPtr XOpenDisplay(string display); | ||
|
||
[DllImport("libX11")] | ||
static extern IntPtr XRootWindow(IntPtr display, int screen); | ||
|
||
try | ||
{ | ||
IntPtr parentWindowHandle = XRootWindow(XOpenDisplay(null), 0); | ||
Func<IntPtr> consoleWindowHandleProvider = () => parentWindowHandle; | ||
|
||
InteractiveBrowserCredentialBrokerOptions brokerOptions = new(parentWindowHandle) | ||
{ | ||
UseDefaultBrokerAccount = true, | ||
}; | ||
|
||
// Create the InteractiveBrowserCredential using broker support | ||
InteractiveBrowserCredential credential = new(brokerOptions); | ||
alexwolfmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Uri vaultUri = new("https://<your-key-vault-name>.vault.azure.net/"); | ||
SecretClient client = new(vaultUri, credential); | ||
|
||
Console.WriteLine("Retrieving secret 'MySecret' from Key Vault..."); | ||
KeyVaultSecret secret = await client.GetSecretAsync("MySecret"); | ||
Console.WriteLine($"Secret value: {secret.Value}"); | ||
|
||
return 0; | ||
} | ||
catch (AuthenticationFailedException ex) | ||
{ | ||
Console.Error.WriteLine($"Authentication failed: {ex.Message}"); | ||
return 2; | ||
} | ||
catch (RequestFailedException ex) | ||
{ | ||
Console.Error.WriteLine($"Key Vault request failed: {ex.Message}"); | ||
return 3; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.Error.WriteLine($"Unexpected error: {ex.Message}"); | ||
return 1; | ||
} |
14 changes: 14 additions & 0 deletions
14
docs/azure/sdk/snippets/authentication/brokered/maui-app/App.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version = "1.0" encoding = "UTF-8" ?> | ||
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:local="clr-namespace:SecretVaultApp" | ||
x:Class="SecretVaultApp.App"> | ||
<Application.Resources> | ||
<ResourceDictionary> | ||
<ResourceDictionary.MergedDictionaries> | ||
<ResourceDictionary Source="resources/Styles/Colors.xaml" /> | ||
<ResourceDictionary Source="resources/Styles/Styles.xaml" /> | ||
</ResourceDictionary.MergedDictionaries> | ||
</ResourceDictionary> | ||
</Application.Resources> | ||
</Application> |
14 changes: 14 additions & 0 deletions
14
docs/azure/sdk/snippets/authentication/brokered/maui-app/App.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace SecretVaultApp; | ||
|
||
public partial class App : Application | ||
{ | ||
public App() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
protected override Window CreateWindow(IActivationState? activationState) | ||
{ | ||
return new Window(new AppShell()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.