快速入门:将应用程序注册到 Microsoft 标识平台Quickstart: Register an application with the Microsoft identity platform

在本快速入门中,你将在 Azure 门户中注册一个应用,以便 Microsoft 标识平台可为该应用程序及其用户提供身份验证和授权服务。In this quickstart, you register an app in the Azure portal so the Microsoft identity platform can provide authentication and authorization services for your application and its users.

Microsoft 标识平台仅对已注册的应用程序执行标识和访问管理 (IAM)。The Microsoft identity platform performs identity and access management (IAM) only for registered applications. 无论是类似于 Web 应用或移动应用的客户端应用程序是,还是支持客户端应用的 Web API,注册它都会在应用程序与标识提供程序(Microsoft 标识平台)之间建立信任关系。Whether it's a client application like a web or mobile app, or it's a web API that backs a client app, registering it establishes a trust relationship between your application and the identity provider, the Microsoft identity platform.

先决条件Prerequisites

注册应用程序Register an application

注册应用程序会在应用与 Microsoft 标识平台之间建立信任关系。Registering your application establishes a trust relationship between your app and the Microsoft identity platform. 信任是单向的:应用信任 Microsoft 标识平台,但标识平台并不信任应用。The trust is unidirectional: your app trusts the Microsoft identity platform, and not the other way around.

按照以下步骤创建应用注册:Follow these steps to create the app registration:

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 如果你有权访问多个租户,请在顶部菜单中,使用“目录 + 订阅”筛选器 选择要在其中注册应用程序的租户。

  3. 搜索并选择“Azure Active Directory” 。Search for and select Azure Active Directory.

  4. 在“管理”下,选择“应用注册” > “新建注册” 。Under Manage, select App registrations > New registration.

  5. 输入应用程序的显示名称。Enter a display Name for your application. 应用程序的用户在使用应用时(例如,在登录过程中)可能会看到显示名称。Users of your application might see the display name when they use the app, for example during sign-in. 你可以随时更改显示名称,多个应用注册可共享同一名称。You can change the display name at any time and multiple app registrations can share the same name. 应用注册自动生成的应用程序(客户端)ID(而不是其显示名称)在标识平台中唯一地标识应用。The app registration's automatically generated Application (client) ID, not its display name, uniquely identifies your app within the identity platform.

  6. 指定可使用该应用程序的人员,这有时称为“登录访问者”。Specify who can use the application, sometimes called its sign-in audience.

    支持的帐户类型Supported account types 说明Description
    仅此组织目录中的帐户Accounts in this organizational directory only 如果要生成仅供租户中的用户(或来宾)使用的应用程序,请选择此选项。Select this option if you're building an application for use only by users (or guests) in your tenant.

    通常称为业务线 (LOB) 应用程序,这是 Microsoft 标识平台中的单租户应用程序 。Often called a line-of-business (LOB) application, this app is a single-tenant application in the Microsoft identity platform.
    任何组织目录中的帐户Accounts in any organizational directory 如果希望任何 Azure Active Directory (Azure AD) 租户中的用户都能够使用你的应用程序,请选择此选项。Select this option if you want users in any Azure Active Directory (Azure AD) tenant to be able to use your application. 例如,如果要构建打算向多个组织提供的软件即服务 (SaaS) 应用程序,则适合使用此选项。This option is appropriate if, for example, you're building a software-as-a-service (SaaS) application that you intend to provide to multiple organizations.

    这类应用在 Microsoft 标识平台中被称为多租户应用程序。This type of app is known as a multitenant application in the Microsoft identity platform.
  7. 请勿对“重定向 URI (可选)”输入任何内容。Don't enter anything for Redirect URI (optional). 你将在下一部分配置重定向 URI。You'll configure a redirect URI in the next section.

  8. 选择“注册”,完成初始应用注册。Select Register to complete the initial app registration.

    Web 浏览器中 Azure 门户的屏幕截图,其中显示了“注册应用程序”窗格。

注册完成后,Azure 门户会显示应用注册的“概述”窗格。When registration finishes, the Azure portal displays the app registration's Overview pane. 你会看到应用程序(客户端)ID。You see the Application (client) ID. 此值也被称为客户端 ID,它可唯一地标识 Microsoft 标识平台中的应用程序。Also called the client ID, this value uniquely identifies your application in the Microsoft identity platform.

应用程序的代码(更多情况下通常是应用程序中使用的身份验证库)也会使用客户端 ID。Your application's code, or more typically an authentication library used in your application, also uses the client ID. 在验证它从标识平台中接收的安全令牌时,会使用此 ID。The ID is used as part of validating the security tokens it receives from the identity platform.

Web 浏览器中 Azure 门户的屏幕截图,其中显示了应用注册的“概述”窗格。

添加重定向 URIAdd a redirect URI

Microsoft 标识平台在重定向 URI 处重定向用户客户端并在身份验证后发送安全令牌。A redirect URI is the location where the Microsoft identity platform redirects a user's client and sends security tokens after authentication.

例如,在生产 Web 应用程序中,重定向 URI 通常是运行应用的公共终结点,比如 https://contoso.com/auth-responseIn a production web application, for example, the redirect URI is often a public endpoint where your app is running, like https://contoso.com/auth-response. 在开发过程中,通常还会添加在本地运行应用的终结点,例如 https://127.0.0.1/auth-responsehttp://localhost/auth-responseDuring development, it's common to also add the endpoint where you run your app locally, like https://127.0.0.1/auth-response or http://localhost/auth-response.

可通过配置已注册应用程序的平台设置来添加和修改它的重定向 URI。You add and modify redirect URIs for your registered applications by configuring their platform settings.

配置平台设置Configure platform settings

在 Azure 门户的平台配置中配置每种应用程序类型的设置(包括重定向 URI)。Settings for each application type, including redirect URIs, are configured in Platform configurations in the Azure portal. 某些平台(例如 Web 和单页应用程序)要求手动指定重定向 URI 。Some platforms, like Web and Single-page applications, require you to manually specify a redirect URI. 对于其他平台(例如移动和桌面),可选择在配置其他设置时为用户生成的重定向 URI。For other platforms, like mobile and desktop, you can select from redirect URIs generated for you when you configure their other settings.

若要根据面向的平台或设备配置应用程序设置:To configure application settings based on the platform or device you're targeting:

  1. 在 Azure 门户中的“应用注册”中,选择你的应用程序。In the Azure portal, in App registrations, select your application.

  2. 在“管理”下,选择“身份验证”。 Under Manage, select Authentication.

  3. 在“平台配置”下,选择“添加平台” 。Under Platform configurations, select Add a platform.

  4. 在“配置平台”下,选择应用程序类型(平台)的磁贴来配置其设置。Under Configure platforms, select the tile for your application type (platform) to configure its settings.

    Azure 门户中的“平台配置”窗格的屏幕截图。

    平台Platform 配置设置Configuration settings
    WebWeb 输入应用的重定向 URI。Enter a Redirect URI for your app. Microsoft 标识平台在此 URI 处重定向用户客户端并在身份验证后发送安全令牌。This URI is the location where the Microsoft identity platform redirects a user's client and sends security tokens after authentication.

    为服务器上运行的标准 Web 应用程序选择此平台。Select this platform for standard web applications that run on a server.
    单页应用程序Single-page application 输入应用的重定向 URI。Enter a Redirect URI for your app. Microsoft 标识平台在此 URI 处重定向用户客户端并在身份验证后发送安全令牌。This URI is the location where the Microsoft identity platform redirects a user's client and sends security tokens after authentication.

    如果要在 JavaScript 中或使用 Angular、Vue.js、React.js 或 Blazor WebAssembly 等框架构建客户端 Web 应用,请选择此平台。Select this platform if you're building a client-side web app by using JavaScript or a framework like Angular, Vue.js, React.js, or Blazor WebAssembly.
    iOS/macOSiOS / macOS 输入应用捆绑 ID。Enter the app Bundle ID. 可在“生成设置”或 Xcode 中的 Info.plist 内找到此信息。Find it in Build Settings or in Xcode in Info.plist.

    指定捆绑 ID 时,系统会为你生成重定向 URI。A redirect URI is generated for you when you specify a Bundle ID.
    AndroidAndroid 输入应用的包名称。Enter the app Package name. 可在 AndroidManifest.xml 文件中找到它。Find it in the AndroidManifest.xml file. 也请生成签名哈希并输入它。Also generate and enter the Signature hash.

    如果指定这些设置,将生成重定向 URI。A redirect URI is generated for you when you specify these settings.
    移动和桌面应用程序Mobile and desktop applications 选择其中一个建议的重定向 URI。Select one of the Suggested redirect URIs. 或者指定自定义重定向 URI。Or specify a Custom redirect URI.

    对于使用嵌入式浏览器的桌面应用程序,我们建议For desktop applications using embedded browser, we recommend
    https://login.partner.microsoftonline.cn/common/oauth2/nativeclient

    对于使用系统浏览器的桌面应用程序,我们建议For desktop applications using system browser, we recommend
    http://localhost

    对于不使用最新的 Microsoft 身份验证库 (MSAL) 或未在使用代理的移动应用程序,请选择此平台。Select this platform for mobile applications that aren't using the latest Microsoft Authentication Library (MSAL) or aren't using a broker. 同时也为桌面应用程序选择此平台。Also select this platform for desktop applications.
  5. 选择“配置”以完成平台配置。Select Configure to complete the platform configuration.

重定向 URI 限制Redirect URI restrictions

对于添加到应用注册的重定向 URI,其格式方面存在某些限制。There are some restrictions on the format of the redirect URIs you add to an app registration. 有关这些限制的详细信息,请参阅重定向 URI(回复 URL)的限制和局限For details about these restrictions, see Redirect URI (reply URL) restrictions and limitations.

添加凭据Add credentials

凭据供访问 Web API 的机密客户端应用程序使用。Credentials are used by confidential client applications that access a web API. Web 应用、其他 Web API服务类型和守护程序类型的应用程序都是机密客户端。Examples of confidential clients are web apps, other web APIs, or service-type and daemon-type applications. 通过凭据,应用程序可以自己的身份进行身份验证,无需用户在运行时进行任何交互。Credentials allow your application to authenticate as itself, requiring no interaction from a user at runtime.

可将证书和客户端密码(字符串)作为凭据添加到机密客户端应用注册。You can add both certificates and client secrets (a string) as credentials to your confidential client app registration.

Azure 门户的屏幕截图,其中显示了应用注册中的“证书和密码”窗格。

添加证书Add a certificate

证书有时也被称为公钥,它是推荐的凭据类型。Sometimes called a public key, a certificate is the recommended credential type. 它比客户端密码提供更多的保障。It provides more assurance than a client secret. 若要详细了解如何在应用程序中使用证书作为身份验证方法,请参阅 Microsoft 标识平台应用程序身份验证证书凭据For more information about using a certificate as an authentication method in your application, see Microsoft identity platform application authentication certificate credentials.

  1. 在 Azure 门户中的“应用注册”中,选择你的应用程序。In the Azure portal, in App registrations, select your application.
  2. 选择“证书和密码” > “上传证书” 。Select Certificates & secrets > Upload certificate.
  3. 选择你想要上传的文件。Select the file you want to upload. 它必须是以下文件类型之一:.cer、.pem、.crt 。It must be one of the following file types: .cer, .pem, .crt.
  4. 选择 添加Select Add.

添加客户端密码Add a client secret

客户端密码也称为应用程序密码。The client secret is also known as an application password. 它是一个字符串值,应用可用它取代证书来标识自身。It's a string value your app can use in place of a certificate to identity itself. 客户端密码是两种凭据类型中更易使用的一种。The client secret is the easier of the two credential types to use. 它常常在开发过程中使用,但它的安全性不如证书。It's often used during development, but it's considered less secure than a certificate. 请在生产环境中运行的应用程序中使用证书。Use certificates in your applications that are running in production.

若要详细了解应用程序安全建议,请参阅 Microsoft 标识平台最佳做法和建议For more information about application security recommendations, see Microsoft identity platform best practices and recommendations.

  1. 在 Azure 门户中的“应用注册”中,选择你的应用程序。In the Azure portal, in App registrations, select your application.
  2. 选择“证书和密码” > “新建客户端密码” 。Select Certificates & secrets > New client secret.
  3. 添加客户端机密的说明。Add a description for your client secret.
  4. 选择持续时间。Select a duration.
  5. 选择 添加Select Add.
  6. 记录机密的值,以便在客户端应用程序代码中使用。Record the secret's value for use in your client application code. 退出此页面后,此机密值永不再显示。This secret value is never displayed again after you leave this page.

后续步骤Next steps

客户端应用程序通常需要访问 Web API 中的资源。Client applications typically need to access resources in a web API. 可使用 Microsoft 标识平台来保护客户端应用程序。You can protect your client application by using the Microsoft identity platform. 还可使用该平台授予对 Web API 的设有范围且基于权限的访问。You can also use the platform for authorizing scoped, permissions-based access to your web API.

转到本系列的下一篇快速入门,为 Web API 再创建一个应用注册并公开其范围。Go to the next quickstart in the series to create another app registration for your web API and expose its scopes.