servicePrincipal: addKey

命名空间:microsoft.graph

将密钥凭据添加到 servicePrincipal。 servicePrincipal 可以使用此方法和 removeKey 自动滚动其过期密钥。

备注

Create servicePrincipal 和更新 servicePrincipal 操作可以继续用于添加和更新具有或不带用户上下文的任何 servicePrincipal 的关键凭据。

作为此方法的请求验证的一部分,将验证拥有现有密钥的证明,然后才能执行该操作。

没有任何现有有效证书的 ServicePrincipals (即尚未添加任何证书,或者所有证书都已过期) ,将不能使用此服务操作。 Update servicePrincipal 可用于执行更新。

权限

权限类型 权限(从最低特权到最高特权)
委派(工作或学校帐户) Application.ReadWrite.All、Directory.ReadWrite.All
委派(个人 Microsoft 帐户) 无。
应用程序 Application.ReadWrite.OwnedBy、Application.ReadWrite.All、Directory.ReadWrite.All

HTTP 请求

POST /servicePrincipals/{id}/addKey

请求标头

名称 说明
Authorization Bearer {token}。必需。
Content-Type application/json. Required.

请求正文

在请求正文中,提供以下必需属性。

属性 类型 说明
keyCredential keyCredential 要添加的新 servicePrincipal 密钥凭据。 类型用法____和密钥 是此用法的必需属性。 受支持的密钥类型包括:
  • AsymmetricX509Cert:用法必须为 Verify
  • X509CertAndPassword:用法必须为 Sign
passwordCredential passwordCredential 仅需要设置应包含密钥密码的 secretText 。 此属性仅对类型 为 的键是必需的 X509CertAndPassword。 否则,设置为 null
proof String 用作现有密钥拥有证明的自签名 JWT 令牌。 必须使用 servicePrincipal 的现有有效证书之一的私钥对此 JWT 令牌进行签名。 令牌应包含以下声明:
  • aud - 受众需要是 00000002-0000-0000-c000-000000000000
  • iss- 颁发者需要是发出调用的 servicePrincipal 的 ID。
  • nbf -“不早于”时间。
  • exp - 过期时间应该是“不早于”+ 10 分钟。

下面是可用于 生成 此拥有令牌证明的代码示例。

响应

如果成功,此方法在响应 200 OK 正文中返回 响应代码和新 keyCredential 对象。

示例

示例 1:将新的密钥凭据添加到 servicePrincipal

请求

下面展示了示例请求。

POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/addKey
Content-type: application/json

{
    "keyCredential": {
        "type": "AsymmetricX509Cert",
        "usage": "Verify",
        "key": "MIIDYDCCAki..."
    },
    "passwordCredential": null,
    "proof":"eyJ0eXAiOiJ..."
}

响应

下面展示了示例响应。

HTTP/1.1 200 OK
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.keyCredential"
}

示例 2:为密钥添加密钥凭据和关联密码

请求

下面展示了示例请求。

POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/addKey
Content-type: application/json

{
    "keyCredential": {
        "type": "X509CertAndPassword",
        "usage": "Sign",
        "key": "MIIDYDCCAki..."
    },
    "passwordCredential": {
        "secretText": "MKTr0w1..."
    },
    "proof":"eyJ0eXAiOiJ..."
}

响应

下面展示了示例响应。

HTTP/1.1 200 OK
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.keyCredential"
}