为滚动密钥生成所有权证明令牌
可以使用在 application 和 servicePrincipal 资源上定义的 addKey 和 removeKey 方法,以编程方式滚动过期密钥。
作为对这些方法的请求验证的一部分,在调用这些方法之前,将对现有密钥的所有权证明进行验证。 该证明由自签名的 JWT 令牌表示。 此 JWT 令牌必须使用应用程序现有有效证书之一的私钥进行签名。 令牌有效期不应超过 10 分钟。
注意: 如果应用程序没有任何现有有效证书 (尚未添加证书,或者所有证书都已) 过期,则无法使用此服务操作。 可改用更新应用程序操作来执行更新。
令牌应包含以下声明:
- aud:受众必须是
00000002-0000-0000-c000-000000000000
。 - iss:颁发者应该是启动请求 的应用程序 或 servicePrincipal 对象的 ID。
- nbf:不是在时间之前。
- exp:到期时间应为 nbf + 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);
}
}
}
还可以使用 Azure KeyVault 中的签名生成证明。 请务必注意,不能在 JWT 标头和有效负载中包含填充字符“=”,否则将返回 Authentication_MissingOrMalformed 错误。
相关内容
拥有所有权证明令牌后,可以将其用于:
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈