您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure Active Directory 库Azure Active Directory libraries for .NET

概述Overview

使用 Azure Active Directory 将用户登录并管理对应用程序和 API 的访问。Sign-on users and manage access to applications and APIs with Azure Active Directory.

若要开始使用 Azure Active Directory,请参阅使用 Azure AD 进行 ASP.NET Web 应用登录和注销To get started with Azure Active Directory, see ASP.NET web app sign-in and sign-out with Azure AD.

客户端库Client library

通过 OAuth2、OpenID Connect、Active Directory 图形 API 身份验证或 SAML 2.0 连接用户或应用程序并对其进行身份验证。Connect and authenticate users or applications over OAuth2, OpenID Connect, Active Directory Graph API authentication or SAML 2.0.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory

.NET Core CLI.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

浏览 Azure Active Directory 代码示例的完整集合。Explore the full collection of Azure Active Directory code samples.