命名空间:microsoft.graph
向 应用程序添加强密码或机密。 还可以 在创建应用程序时添加密码。
此 API 可用于以下国家级云部署。
全局服务 |
美国政府 L4 |
美国政府 L5 (DOD) |
由世纪互联运营的中国 |
✅ |
✅ |
✅ |
✅ |
权限
为此 API 选择标记为最低特权的权限。
只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。
权限类型 |
最低特权权限 |
更高特权权限 |
委派(工作或学校帐户) |
Application.ReadWrite.All |
Directory.ReadWrite.All |
委派(个人 Microsoft 帐户) |
Application.ReadWrite.All |
不可用。 |
应用程序 |
Application.ReadWrite.OwnedBy |
Application.ReadWrite.All、Directory.ReadWrite.All |
HTTP 请求
可以使用其 ID 或 appId 对应用程序进行寻址。
id 和 appId 在 Microsoft Entra 管理中心 中的应用注册中分别称为“对象 ID”和“应用程序 (客户端) ID”。
POST /applications/{id}/addPassword
POST /applications(appId='{appId}')/addPassword
名称 |
说明 |
Authorization |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
application/json. 必需。 |
请求正文
在请求正文中,提供具有以下属性的可选 passwordCredential
对象。
属性 |
类型 |
说明 |
displayName |
String |
密码的友好名称。 可选。 |
endDateTime |
DateTimeOffset |
密码过期的日期和时间使用 ISO 8601 格式表示,并且始终以 UTC 时间表示。 例如,2014 年 1 月 1 日午夜 UTC 为 2014-01-01T00:00:00Z 。 可选。 默认值为“startDateTime + 2 年”。 |
startDateTime |
DateTimeOffset |
密码生效的日期和时间。 时间戳类型表示采用 ISO 8601 格式的日期和时间信息,始终采用 UTC 时区。 例如,2014 年 1 月 1 日午夜 UTC 为 2014-01-01T00:00:00Z 。 可选。 默认值为“now”。 |
响应
如果成功,此方法在响应正文中返回响应 200 OK
代码和新的 passwordCredential 对象。 响应对象中的 secretText 属性包含Microsoft Entra ID生成的强密码/机密,长度为 16-64 个字符。 将来无法检索此密码。
示例
以下示例演示如何调用此 API。
请求
以下示例显示了一个请求。 在请求中指定的 ID 是应用程序的 id 属性的值,而不是 appId 属性的值。
POST https://graph.microsoft.com/v1.0/applications/{id}/addPassword
Content-type: application/json
{
"passwordCredential": {
"displayName": "Password friendly name"
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Applications.Item.AddPassword;
using Microsoft.Graph.Models;
var requestBody = new AddPasswordPostRequestBody
{
PasswordCredential = new PasswordCredential
{
DisplayName = "Password friendly name",
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Applications["{application-id}"].AddPassword.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc applications add-password post --application-id {application-id} --body '{\
"passwordCredential": {\
"displayName": "Password friendly name"\
}\
}\
'
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphapplications "github.com/microsoftgraph/msgraph-sdk-go/applications"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphapplications.NewAddPasswordPostRequestBody()
passwordCredential := graphmodels.NewPasswordCredential()
displayName := "Password friendly name"
passwordCredential.SetDisplayName(&displayName)
requestBody.SetPasswordCredential(passwordCredential)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
addPassword, err := graphClient.Applications().ByApplicationId("application-id").AddPassword().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.applications.item.addpassword.AddPasswordPostRequestBody addPasswordPostRequestBody = new com.microsoft.graph.applications.item.addpassword.AddPasswordPostRequestBody();
PasswordCredential passwordCredential = new PasswordCredential();
passwordCredential.setDisplayName("Password friendly name");
addPasswordPostRequestBody.setPasswordCredential(passwordCredential);
PasswordCredential result = graphClient.applications().byApplicationId("{application-id}").addPassword().post(addPasswordPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const passwordCredential = {
passwordCredential: {
displayName: 'Password friendly name'
}
};
await client.api('/applications/{id}/addPassword')
.post(passwordCredential);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Applications\Item\AddPassword\AddPasswordPostRequestBody;
use Microsoft\Graph\Generated\Models\PasswordCredential;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AddPasswordPostRequestBody();
$passwordCredential = new PasswordCredential();
$passwordCredential->setDisplayName('Password friendly name');
$requestBody->setPasswordCredential($passwordCredential);
$result = $graphServiceClient->applications()->byApplicationId('application-id')->addPassword()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Applications
$params = @{
passwordCredential = @{
displayName = "Password friendly name"
}
}
Add-MgApplicationPassword -ApplicationId $applicationId -BodyParameter $params
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.applications.item.add_password.add_password_post_request_body import AddPasswordPostRequestBody
from msgraph.generated.models.password_credential import PasswordCredential
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = AddPasswordPostRequestBody(
password_credential = PasswordCredential(
display_name = "Password friendly name",
),
)
result = await graph_client.applications.by_application_id('application-id').add_password.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
以下示例显示了相应的响应。
HTTP/1.1 200 OK
Content-type: application/json
{
"customKeyIdentifier": null,
"endDateTime": "2021-09-09T19:50:29.3086381Z",
"keyId": "f0b0b335-1d71-4883-8f98-567911bfdca6",
"startDateTime": "2019-09-09T19:50:29.3086381Z",
"secretText": "[6gyXA5S20@MN+WRXAJ]I-TO7g1:h2P8",
"hint": "[6g",
"displayName": "Password friendly name"
}