大网数据广告活动
Microsoft Graph 接口

生成用于滚动密钥的所有权证明令牌

· 52CE 我爱测

作为对 addKey 和 removeKey 方法的请求验证的一部分,需要提供所有权证明令牌。 本文档提供生成所有权证明令牌的指南。

可以使用在 application 和 servicePrincipal 资源上定义的 addKey 和 removeKey 方法,以编程方式滚动过期密钥。

作为对这些方法的请求验证的一部分,在调用这些方法之前,将对现有密钥的所有权证明进行验证。 该证明由自签名的 JWT 令牌表示。 此 JWT 令牌必须使用应用程序现有有效证书之一的私钥进行签名。 令牌有效期不应超过 10 分钟。

注意: 没有任何现有有效证书(尚未添加证书,或者所有证书均已过期)的应用程序将无法使用此服务操作。 可改用更新应用程序操作来执行更新。

令牌应包含以下声明:

  • aud - 受众需要是 00000002-0000-0000-c000-000000000000。
  • iss - 颁发者需要是正在进行调用的应用程序的 Azure AD ObjectId(而非 applicationId 或 clientId)。
  • nbf -“不早于”时间。
  • exp - 过期时间应该是“不早于”+ 10 分钟。

可使用以下代码示例来生成此所有权证明令牌。

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.JsonWebTokens;

namespace MicrosoftIdentityPlatformProofTokenGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure the following
            string pfxFilePath = "<Path to your certificate file";
            string password = "<Certificate password>";
            string objectId = "<id of the application or servicePrincipal object>";

            // Get signing certificate
            X509Certificate2 signingCert = new X509Certificate2(pfxFilePath, password);

            // audience
            string aud = $"00000002-0000-0000-c000-000000000000";

            // aud and iss are the only required claims.
            var claims = new Dictionary<string, object>()
            {
                { "aud", aud },
                { "iss", objectId }
            };

            // token validity should not be more than 10 minutes
            var now = DateTime.UtcNow;
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Claims = claims,
                NotBefore = now,
                Expires = now.AddMinutes(10),
                SigningCredentials = new X509SigningCredentials(signingCert)
            };

            var handler = new JsonWebTokenHandler();
            var x = handler.CreateToken(securityTokenDescriptor);
            Console.WriteLine(x);
        }
    }
}

注意: 可以使用其他工具(如 PowerShell 或使用 Azure KeyVault 的签名)生成证明。 请务必注意,JWT 标头和有效负载中不得包含填充字符“=”,否则将返回 Authentication_MissingOrMalformed 错误。

历史资料。此文对应 2023-02-03 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者(原始作者元数据 FaithOmbongi)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / Microsoft Graph 中文文档历史快照
适用范围
历史中文快照 2023-02-03;详见原文产品版本;文档日期 未标明
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。