快速入门:从 ASP.NET Core Web 应用将用户登录并调用 Microsoft Graph API

本快速入门使用示例 ASP.NET Core Web 应用程序展示如何使用授权代码流登录用户并调用 Microsoft Graph API。 该示例使用适用于 .NET 的 Microsoft 身份验证库Microsoft 标识 Web 来处理身份验证。

先决条件

注册应用程序和记录标识符

若要完成注册,请为应用程序命名并指定支持的帐户类型。 注册后,应用程序“概述”窗格将显示应用程序源代码中所需的标识符。

  1. 登录到 Microsoft Entra 管理中心

  2. 如果你有权访问多个租户,请使用顶部菜单中的“设置”图标 ,通过“目录 + 订阅”菜单切换到你希望在其中注册应用程序的租户。

  3. 浏览至“标识”>“应用程序”>“应用注册”,然后选择“新建注册”。

  4. 输入应用程序名称,例如 identity-client-web-app

  5. 对于“支持的帐户类型”设置,请选择“仅限此组织目录中的帐户”。 要了解不同帐户类型的信息,请选择“帮我选择”选项。

  6. 选择“注册”。

    显示了如何在 Microsoft Entra 管理中心输入名称和选择帐户类型的屏幕截图。

  7. 注册完成后,将显示应用程序的“概述”窗格。 记录要在应用程序源代码中使用的目录(租户)ID 和应用程序(客户端)ID。

    显示了 Microsoft Entra 管理中心的概述页面上的标识符值的屏幕截图。

    注意

    可以通过参照修改应用程序支持的帐户来更改支持的帐户类型。

添加平台重定向 URI

若要为应用注册指定应用类型,请执行以下步骤:

  1. 在“管理”下,选择“身份验证”。
  2. 在“平台配置”页上,选择“添加平台”,然后选择“Web”选项。
  3. 对于“重定向 URI”,请输入 https://localhost:5001/signin-oidc
  4. 在“前通道注销 URL”下,输入 https://localhost:5001/signout-callback-oidc 进行退出登录。
  5. 选择“配置”以保存更改。

克隆或下载示例应用程序

若要获取示例应用程序,可以从 GitHub 克隆它或将其下载为 .zip 文件。

  • 若要克隆示例,请打开命令提示符并导航到要创建项目的位置,然后输入以下命令:

    git clone https://github.com/Azure-Samples/ms-identity-docs-code-dotnet.git
    
  • 下载 .zip 文件。 将其提取到名称长度小于 260 个字符的文件路径。

创建并上传自签名证书

  1. 使用终端,通过以下命令导航以在项目目录中创建自签名证书。

    cd ms-identity-docs-code-dotnet\web-app-aspnet\
    dotnet dev-certs https -ep ./certificate.crt --trust
    
  2. 返回 Microsoft Entra 管理中心,在“管理”下,选择“证书和机密”>“上传证书”

  3. 选择“证书(0)”选项卡,然后选择“上传证书”。

  4. 此时会显示“上传证书”窗格。 使用该图标导航到在上一步中创建的证书文件,然后选择“打开”。

  5. 输入证书的说明,例如 aspnet-web-app 的证书,然后选择“添加”。

  6. 记录指纹值以在下一步中使用。

配置项目

  1. 在 IDE 中,打开包含示例的项目文件夹 ms-identity-docs-code-dotnet\web-app-aspnet
  2. 打开 appsettings.json 并将文件内容替换为以下代码片段;
{
"AzureAd": {
  "Instance": "https://login.partner.microsoftonline.cn/",
  "TenantId": "Enter the tenant ID obtained from the Microsoft Entra admin center",
  "ClientId": "Enter the client ID obtained from the Microsoft Entra admin center",
  "ClientCertificates": [
    {
      "SourceType": "StoreWithThumbprint",
      "CertificateStorePath": "CurrentUser/My",
      "CertificateThumbprint": "Enter the certificate thumbprint obtained the Microsoft Entra admin center"
    }   
  ],
  "CallbackPath": "/signin-oidc"
},
  "DownstreamApi": {
    "BaseUrl": "https://microsoftgraph.chinacloudapi.cn/v1.0/",
    "RelativePath": "me",
    "Scopes": [ 
    "https://microsoftgraph.chinacloudapi.cn/user.read" 
    ]
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
* `TenantId` - The identifier of the tenant where the application is registered. Replace the text in quotes with the `Directory (tenant) ID` that was recorded earlier from the overview page of the registered application.
* `ClientId` - The identifier of the application, also referred to as the client. Replace the text in quotes with the `Application (client) ID` value that was recorded earlier from the overview page of the registered application.
* `ClientCertificates` - A self-signed certificate is used for authentication in the application. Replace the text of the `CertificateThumbprint` with the thumbprint of the certificate that was previously recorded.

运行应用程序并登录

  1. 在项目目录中,使用终端输入以下命令;

    dotnet run
    
  2. 复制终端中显示的 https URL(例如 https://localhost:5001),并将其粘贴到浏览器中。 建议使用私密浏览器或无痕浏览器会话。

  3. 按照步骤操作并输入必要的详细信息以使用 Microsoft 帐户登录。 系统会要求你提供电子邮件地址,以便向你发送一次性密码。 出现提示时输入代码。

  4. 该应用程序将请求权限,以保持访问你已授权访问的数据、登录和阅读配置文件的权限。 选择“接受”。

  5. 将显示以下屏幕截图,表明你已登录应用程序且已从 Microsoft Graph API 访问个人资料详细信息。

    描述 API 调用结果的屏幕截图。

退出登录应用程序

  1. 在页面右上角找到“退出登录”链接,然后选择它。
  2. 系统将提示你选取要退出登录的帐户。 选择用于登录的帐户。
  3. 此时会显示一条消息,指示你已退出登录。现在可以关闭浏览器窗口。