《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