用于调用 Web API 的守护程序应用 - 获取令牌

在构建机密客户端应用程序以后,可以获取应用的令牌,方法是调用 AcquireTokenForClient,传递作用域,然后强制刷新令牌。

请求的作用域

请求客户端凭据流时,其作用域是资源的名称后跟 /.default。 这种表示法告知 Microsoft Entra ID 使用在应用程序注册过程中静态声明的应用程序级权限。 另外,这些 API 权限必须由租户管理员授予。

下面是将 Web API 的范围定义为 appsettings.json 文件中的部分配置的示例。 此示例摘自 GitHub 上的 .NET 控制台守护程序代码示例。

{
    "AzureAd": {
        // Same AzureAd section as before.
    },

    "MyWebApi": {
        "BaseUrl": "https://localhost:44372/",
        "RelativePath": "api/TodoList",
        "RequestAppToken": true,
        "Scopes": [ "[Enter here the scopes for your web API]" ]
    }
}

Azure AD (v1.0) 资源

用于客户端凭据的作用域应该始终为资源 ID 后跟 /.default

重要

当 MSAL 为接受 1.0 版访问令牌的资源请求访问令牌时,Microsoft Entra ID 会获取最后一个斜杠前面的所有内容并将其用作资源标识符,从请求的范围内分析所需的受众。 因此,就像 Azure SQL 数据库 (https://database.chinacloudapi.cn) 一样,如果资源需要以斜杠结尾的受众(对于 Azure SQL 数据库为 https://database.chinacloudapi.cn/),你将需要请求作用域 https://database.chinacloudapi.cn//.default。 (注意双斜杠。)另请参阅 MSAL.NET 问题 #747:Resource url's trailing slash is omitted, which caused sql auth failure

AcquireTokenForClient API

为了获取应用的令牌,请使用 AcquireTokenForClient 或其等效命令,具体取决于平台。

使用 Microsoft.Identity.Web 时,无需获取令牌。 可以使用更高级别的 API,如从守护程序应用程序调用一个 Web API中所示。 但是,如果使用的 SDK 需要令牌,以下代码片段则会显示如何获取此令牌。

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Web;

// In the Program.cs, acquire a token for your downstream API

var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
ITokenAcquirer acquirer = tokenAcquirerFactory.GetTokenAcquirer();
AcquireTokenResult tokenResult = await acquirer.GetTokenForUserAsync(new[] { "https://microsoftgraph.chinacloudapi.cn/.default" });
string accessToken = tokenResult.AccessToken;

协议

如果还没有适用于所选语言的库,你可能希望直接使用协议:

第一种情况:使用共享机密访问令牌请求

POST /{tenant}/oauth2/v2.0/token HTTP/1.1           //Line breaks for clarity.
Host: login.partner.microsoftonline.cn
Content-Type: application/x-www-form-urlencoded

client_id=535fb089-9ff3-47b6-9bfb-4f1264799865
&scope=https%3A%2F%2Fmicrosoftgraph.chinacloudapi.cn%2F.default
&client_secret=qWgdYAmab0YSkuL1qKv5bPX
&grant_type=client_credentials

第二种情况:使用证书访问令牌请求

POST /{tenant}/oauth2/v2.0/token HTTP/1.1               // Line breaks for clarity.
Host: login.partner.microsoftonline.cn
Content-Type: application/x-www-form-urlencoded

scope=https%3A%2F%2Fmicrosoftgraph.chinacloudapi.cn%2F.default
&client_id=97e0a5b7-d745-40b6-94fe-5f77d35c6e05
&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
&client_assertion=eyJhbGciOiJSUzI1NiIsIng1dCI6Imd4OHRHeXN5amNScUtqRlBuZDdSRnd2d1pJMCJ9.eyJ{a lot of characters here}M8U3bSUKKJDEg
&grant_type=client_credentials

有关详细信息,请参阅协议文档:Microsoft 标识平台和 OAuth 2.0 客户端凭据流

故障排除

你是否使用过 resource/.default 作用域?

如果出现一条错误消息,指出所使用的作用域无效,则表明你并未使用 resource/.default 作用域。

如果在调用 API 时出现错误“权限不足,无法完成该操作”, 则租户管理员需要授予对应用程序的权限。 有关如何为应用程序授予管理员同意的指导,请参 快速入门:获取令牌并在 .NET 控制台应用中调用 Microsoft Graph 中的步骤 4。

如果未向应用程序授予管理员同意,将遇到以下错误:

Failed to call the web API: Forbidden
Content: {
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Insufficient privileges to complete the operation.",
    "innerError": {
      "request-id": "<guid>",
      "date": "<date>"
    }
  }
}

是否在调用自己的 API?

如果你的守护程序应用调用你自己的 Web API,并且你无法向守护程序的应用注册添加应用权限,则需要将应用角色添加到 Web API 的应用注册

后续步骤

转到此方案中的下一篇文章:调用 Web API