Delen via


Azure Identity Brokered Authentication client library for .NET - version 1.2.0

The library extends the Azure.Identity library to provide authentication broker support. It includes the necessary dependencies and provides the InteractiveBrowserCredentialBrokerOptions class. This options class can be used to create an InteractiveBrowserCredential capable of using the system authentication broker in lieu of an embedded web view or the system browser.

Source code | Package (NuGet) | API reference documentation | Microsoft Entra ID documentation

Getting started

Install the package

Install the Azure Identity client library for .NET with NuGet:

dotnet add package Azure.Identity.Broker

Prerequisites

The Azure.Identity library is a dependency of Azure.Identity.Broker.

Authenticate the client

Key concepts

This package enables authentication broker support via InteractiveBrowserCredentialBrokerOptions, in combination with InteractiveBrowserCredential in the Azure.Identity package.

Parent window handles

When authenticating interactively via InteractiveBrowserCredential constructed with the InteractiveBrowserCredentialBrokerOptions, a parent window handle is required to ensure that the authentication dialog is shown correctly over the requesting window. In the context of graphical user interfaces on devices, a window handle is a unique identifier that the operating system assigns to each window. For the Windows operating system, this handle is an integer value that serves as a reference to a specific window.

Microsoft account (MSA) passthrough

Microsoft accounts (MSA) are personal accounts created by users to access Microsoft services. MSA passthrough is a legacy configuration which enables users to get tokens to resources which normally don't accept MSA logins. This feature is only available to first-party applications. Users authenticating with an application that is configured to use MSA passthrough can set the InteractiveBrowserCredentialBrokerOptions.IsLegacyMsaPassthroughEnabled property to true to allow these personal accounts to be listed by WAM.

Redirect URIs

Microsoft Entra applications rely on redirect URIs to determine where to send the authentication response after a user has logged in. To enable brokered authentication through WAM, a redirect URI matching the following pattern should be registered to the application:

ms-appx-web://Microsoft.AAD.BrokerPlugin/{client_id}

Examples

Configure the InteractiveBrowserCredential to use the system authentication broker

This example demonstrates configuring the InteractiveBrowserCredential with the specialized options type InteractiveBrowserCredentialBrokerOptions to enable brokered authentication.

IntPtr parentWindowHandle = GetForegroundWindow();

// Create an interactive browser credential which will use the system authentication broker
var credential = new InteractiveBrowserCredential(
    new InteractiveBrowserCredentialBrokerOptions(parentWindowHandle));

// Use the credential to authenticate a secret client
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), credential);

To bypass the account selection dialog and use the default broker account, set the InteractiveBrowserCredentialBrokerOptions.UseDefaultBrokerAccount property:

var credential = new InteractiveBrowserCredential(
    new InteractiveBrowserCredentialBrokerOptions(parentWindowHandle)
    {
        UseDefaultBrokerAccount = true,
    });

Troubleshooting

See the troubleshooting guide for details on how to diagnose various failure scenarios.

Error handling

Errors arising from authentication can be raised on any service client method which makes a request to the service. This is because the first time the token is requested from the credential is on the first call to the service, and any subsequent calls might need to refresh the token. In order to distinguish these failures from failures in the service client Azure Identity classes raise the AuthenticationFailedException with details to the source of the error in the exception message as well as possibly the error message. Depending on the application these errors may or may not be recoverable.

using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

// Create a secret client using the DefaultAzureCredential
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());

try
{
    KeyVaultSecret secret = await client.GetSecretAsync("secret1");
}
catch (AuthenticationFailedException e)
{
    Console.WriteLine($"Authentication Failed. {e.Message}");
}

For more details on dealing with errors arising from failed requests to Microsoft Entra ID, or managed identity endpoints please refer to the Microsoft Entra ID documentation on authorization error codes.

Logging

The Azure Identity library provides the same logging capabilities as the rest of the Azure SDK.

The simplest way to see the logs to help debug authentication issues is to enable the console logging.

// Setup a listener to monitor logged events.
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();

All credentials can be configured with diagnostic options, in the same way as other clients in the SDK.

DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions()
{
    Diagnostics =
    {
        LoggedHeaderNames = { "x-ms-request-id" },
        LoggedQueryParameters = { "api-version" },
        IsLoggingContentEnabled = true
    }
};

CAUTION: Requests and responses in the Azure Identity library contain sensitive information. Precaution must be taken to protect logs when customizing the output to avoid compromising account security.

Thread safety

We guarantee that all credential instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing credential instances is always safe, even across threads.

Additional concepts

Client options | Accessing the response | Diagnostics | Mocking | Client lifetime

Next steps

Client libraries supporting authentication with Azure Identity

Many of the client libraries listed here support authenticating with TokenCredential and the Azure Identity library. There you will also find links where you can learn more about their use, including additional documentation and samples.

Known issues

This library does not currently support scenarios relating to the AAD B2C service.

Currently open issues for the Azure.Identity library can be found here.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions