Azure Active Directory libraries for .NET

Overview

Sign-on users and manage access to applications and APIs with Azure Active Directory.

To get started with Azure Active Directory, see ASP.NET web app sign-in and sign-out with Azure AD.

Client library

Connect and authenticate users or applications over OAuth2, OpenID Connect, Active Directory Graph API authentication or SAML 2.0.

Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio Package Manager

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory

.NET Core CLI

dotnet add package Microsoft.IdentityModel.Clients.ActiveDirectory

Code Example

Retrieve an access token for a desktop application.

/* Include this "using" directive...
using Microsoft.IdentityModel.Clients.ActiveDirectory;
*/

AuthenticationResult result = null;
AuthenticationContext authContext = new AuthenticationContext("https://someauthority.com");
try
{
    result = await authContext.AcquireTokenAsync(graphResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
}
catch (AdalException ex)
{
    // An unexpected error occurred, or user canceled the sign in.
    if (ex.ErrorCode != "access_denied")
        MessageBox.Show(ex.Message);

    return;
}

Samples

Explore the full collection of Azure Active Directory code samples.