在应用程序注册上调用 addKey 时出现 Authentication_MissingOrMalformed

问题描述 投票:0回答:1

我几天来一直在尝试使用 此 API 轮换密钥进行测试。但无论我怎么做,我总是会得到 “Authentication_MissingOrMalformed”和错误代码 401。我还需要做什么吗?

我首先创建了一个具有以下权限的 Azure AD 应用程序注册: application permissions

然后,我按照本指南创建了一个自签名证书并将其上传以用作客户端证书。

然后,我按照此处指定的指南生成证明。

string pfxFilePath = "certandkey.pfx";
string password = "password";
string objectId = {objectId}";

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

// audience
string aud = $"00000003-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)

然后我生成了一个新的自签名证书,我想添加该证书,并将其导出到另一个文件。然后,我尝试按如下方式调用 API,但无法克服我看到的异常。

string tenantId = "{tenantId}";
string clientId = "{clientId}";
string newCertPath = "newcert.pfx";
X509Certificate2 newCert = new X509Certificate2(newCertPath);
var credential = new ClientCertificateCredential(tenantId, clientId, signingCert);
var graphClient = new GraphServiceClient(credential);

var requestBody = new Microsoft.Graph.Applications.Item.AddKey.AddKeyPostRequestBody
{
    KeyCredential = new KeyCredential
    {
        Type = "AsymmetricX509Cert",
        Usage = "Verify",
        Key = newCert.GetRawCertData()
    },
    PasswordCredential = null,
    Proof = x,
};
var result = await graphClient.Applications[objectId].AddKey.PostAsync(requestBody);

我真的很感谢一些帮助来找出我在这里做错了什么。我已经使用 C# 和 Java 尝试过此操作,并导致了相同的错误。不应该有任何权限错误,因为根据文档,我不需要任何额外的权限来执行此操作。

permission documentation

谢谢

microsoft-graph-api azure-app-registration
1个回答
0
投票

受众必须是“00000002-0000-0000-c000-000000000000” - 文档被错误地更新为使用新的 Microsoft Graph 端点的客户端 ID,而该端点特别需要 Azure AD Graph - 此后已修复: https://github.com/microsoftgraph/microsoft-graph-docs-contrib/commit/03bf982d8b96b400f2d178b195fee8af9f93521b

© www.soinside.com 2019 - 2024. All rights reserved.