Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  《Windows Azure Platform 系列文章目录

 

  我们在使用Azure Service Principle,通过应用程序开发API方式部署或修改Azure资源的时候,默认的Client Secret过期时间为2年。

  很多客户希望Client Secret过期时间大于2年。

  我们可以通过使用应用管理策略 (App Management Policy),来设置Client Secret过期时间策略。

  应用管理策略现在是正式商用阶段(General Available),可以直接用于生产环境。

 

  注意事项:使用应用管理策略 (App Management Policy):

  - 无法把之前创建的、老的Client Secret的2年有效期直接延长到100年

  - 只能新增1个新的Client Secret,新的Client Secret有效期为100年

  - 老的Client Secret (2年)和新的Client Secret (100年),同时生效。

  - 笔者不建议直接删除老的Client Secret (有效期2年),建议测试新的Client Secret (100年)

 

  主要有三个主要步骤:

  一. 申请API权限

  二. 申请测试License

  三. 使用Postman创建新的100年的Client Secret

 

  一. 申请API权限

  1.假设我们已经有一个Azure Service Principle,命名为mysso。Client Secret默认过期时间为2年。如下图2024年12月7日。

  

  2.我们首先点击mysso,点击API Permission,点击 + Add Permission

  

 

  3.在右侧窗口中,选择Microsoft APIs,点击Microsoft Graph

  

 

  4.选择Application Permission,搜索并选择以下Permission:Policy.Read.All,Policy.ReadWrite.ApplicationConfiguration,Application.ReadWrite.All

  5.设置成功后,我们点击下图Grant consent for contoso,允许这些Permission

  

  执行成功后,Status都会变成绿色

  

 

  二.申请测试License

  1.更新App Management Policy,还需要申请额外的License,我们这里使用测试License,不会收取额外的费用

  2.建议在申请完测试License后,直接使用Postman进行测试。针对整个Tenant启用密码有效期100年

  3.启用完毕后,后续就不再需要使用Azure测试License。测试License到期不需要续费。

  4.我们搜索Azure Entra ID,点击下图的Licenses

  

  5.页面跳转后,点击下图的All Production, Try/Buy,然后点击Purchase services

  

  6.页面跳转后,我们进行登录,步骤略。

  7.在Purchase Service页面里,搜素关键字:Microsoft Entra Workload ID

  

 

  8.鼠标往下拉,在Security and Identity里找到相关信息,点击下图的Details

  

  9.页面跳转后,点击下图的Start Free Trial

  

 

  10.后面的步骤略。

 

  三.使用Postman进行测试

  1.首先我们先获得Service Principle的Token信息:

Request URL

https://login.microsoftonline.com/[TenantID]/oauth2/v2.0/token

 

比如:https://login.microsoftonline.com/d51bf35e-c5bb-4830-aada-f3d6a3a89119/oauth2/v2.0/token

Request Type

POST

Request Body

tenant: 【这里需要输入】

client_id: 【这里需要输入】

scope:https://graph.microsoft.com/.default

client_secret:【这里需要输入】

grant_type:client_credentials

   Response Body会返回Access Token

 

  2.然后我们查看默认的App Management Policy

  

Request URL

https://graph.microsoft.com/v1.0/policies/defaultAppManagementPolicy

Request Type

Get

Request Header

Authorization Bearer {token}。必需。

Content-Type:application/json

  这里的{token}就是我们在步骤1里返回的Access Token

  返回的Response Body会显示默认的App Management Policy

  

 

 

  3.我们修改这个默认的App Management Policy为100年

  这里的{token}就是我们在步骤6里返回的Access Token

  

Request URL

https://graph.microsoft.com/v1.0/policies/defaultAppManagementPolicy

Request Type

PATCH

Request Header

Authorization Bearer {token}。必需。

Content-Type:application/json

Request Body

{

    "isEnabled": true,

    "applicationRestrictions": {

        "passwordCredentials": [

            {

                "restrictionType": "passwordLifetime",

                "maxLifetime": "P100Y",

                "restrictForAppsCreatedAfterDateTime": "2017-01-01T10:37:00Z"

            },

            {

                "restrictionType": "symmetricKeyAddition",

                "maxLifetime": "P100Y",

                "restrictForAppsCreatedAfterDateTime": "2021-01-01T10:37:00Z"

            },

            {

                "restrictionType": "customPasswordAddition",

                "maxLifetime": "P100Y",

                "restrictForAppsCreatedAfterDateTime": "2015-01-01T10:37:00Z"

            },

            {

                "restrictionType": "symmetricKeyLifetime",

                "maxLifetime": "P100Y",

                "restrictForAppsCreatedAfterDateTime": "2015-01-01T10:37:00Z"

            }

        ]

    }

}

 

  

  如果成功,此方法会返回204 No Content响应代码

 

  4.最后,我们新增一个Key,过期时间为100年。我们先查看之前的应用注册mysso的Object ID为0c0开头。如下图:

  

 

  调用的API信息:

  

Request URL

https://graph.microsoft.com/v1.0/applications/[AppRegistration的ObjectID]/addPassword

Request Type

POST

Request Header

Authorization Bearer {token}。必需。

Content-Type:application/json

Request Body

{

    "passwordCredential": {

        "displayName": "显示名称",

        "endDateTime": " key的有效期过期时间",

        "startDateTime": "key的有效期开始时间"

    }

}

 

举个例子:

{

    "passwordCredential": {

        "displayName": "Password100Years",

        "startDateTime": "2023-01-10T00:00:00.0000000Z",

        "endDateTime": "2122-01-09T00:00:00.0000000Z"

    }

}

 

  执行成功后,会在POSTMAN里获得新的,过期时间为100年的Access Key。请保存这个Access Key后续使用。如下图:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.passwordCredential",
    "customKeyIdentifier": null,
    "displayName": "Password100Years",
    "endDateTime": "2122-01-09T00:00:00Z",
    "hint": "r6i",
    "keyId": "6b059f6d-7959-4727-80d9-f54336180438",
    "secretText": "r6i8Q~fw7v5Zn1MYjbEuTtNkcy7_PBs4eabcdefg",
    "startDateTime": "2023-01-10T00:00:00Z"
}

 

  并且我们在Azure Active Directory中,可以查看到这个过期时间为100年的Access Key

  

 

  5.执行完毕后,我们之前创建的2年内过期的key和100年过期的key会同时生效。

  这里的{token}就是我们在步骤6里返回的Access Token

 

posted on 2024-03-18 17:27  Lei Zhang的博客  阅读(67)  评论(0编辑  收藏  举报