共用方式為


取得現有的服務主體

列出服務主體

如果您已經有想要使用的現有服務主體,此步驟說明如何擷取現有的服務主體。

您可以使用 az ad sp list 來擷取 租使用者中的服務主體清單。 根據預設,此命令會傳回租戶的前 100 個服務主體。 若要取得租使用者的所有服務主體,請使用 --all 參數。 取得此清單可能需要很長的時間,因此建議您使用下列其中一個參數來篩選清單:

  • --display-name 要求具有與所提供名稱開頭相匹配的服務主體。 服務主體的顯示名稱是建立期間使用 --name 參數所設定的值。 如果您在服務主體建立期間未設定 --name ,則名稱前置詞為 azure-cli-
  • --spn 篩選符合服務主體名稱精確匹配。 服務主體名稱一律以 https://開頭。 如果您所使用的 --name 值不是 URI,則此值會是 https://,後面接著顯示名稱。
  • --show-mine 只會要求由登入使用者建立的服務主體。
  • --filter 會採用 OData 篩選,並執行 伺服器端 篩選。 建議使用此方法,而不是透過 CLI 的 --query 參數進行用戶端篩選。 若要瞭解 OData 篩選條件,請參閱 篩選的 OData 表達式語法。

傳回的服務主體對象資訊過於詳盡。 若要只取得登入所需的資訊,請使用查詢字串 [].{id:appId, tenant:appOwnerOrganizationId}。 以下範例會取得目前登入使用者所建立之所有服務主體的登入資訊:

az ad sp list --show-mine --query "[].{SPname:displayName, SPid:appId, tenant:appOwnerOrganizationId}" --output table

如果您在具有許多服務主體的大型組織中工作,請嘗試下列命令範例:

# get service principals containing a keyword
az ad sp list --display-name mySearchWord --output table

# get service principals using an OData filter
az ad sp list --filter "displayname eq 'myExactServicePrincipalName'" --output json

# get a service principal having a certain servicePrincipalNames property value
az ad sp list --spn https://spURL.com

這很重要

使用者和租戶都可以使用 az ad sp listaz ad sp show 來擷取,但驗證秘密或驗證方法無法使用。 您可以使用 az keyvault secret show 來擷取 Azure 金鑰保存庫 中憑證的秘密,但預設不會儲存其他秘密。 如果您忘記驗證方法或秘密,請 重設服務主體認證

服務主體屬性

當您使用 az ad sp list取得服務主體清單時,腳本中有許多輸出屬性可供參考。

[
  {
    "accountEnabled": true,
    "addIns": [],
    "alternativeNames": [],
    "appDescription": null,
    "appDisplayName": "myServicePrincipalName",
    "appId": "00000000-0000-0000-0000-000000000000",
    "appOwnerOrganizationId": "00000000-0000-0000-0000-000000000000",
    "appRoleAssignmentRequired": false,
    "appRoles": [],
    "applicationTemplateId": null,
    "createdDateTime": null,
    "deletedDateTime": null,
    "description": null,
    "disabledByMicrosoftStatus": null,
    "displayName": "myServicePrincipalName",
    "homepage": "https://myURL.com",
    "id": "00000000-0000-0000-0000-000000000000",
    "info": {
      "logoUrl": null,
      "marketingUrl": null,
      "privacyStatementUrl": null,
      "supportUrl": null,
      "termsOfServiceUrl": null
    },
    "keyCredentials": [],
    "loginUrl": null,
    "logoutUrl": null,
    "notes": null,
    "notificationEmailAddresses": [],
    "oauth2PermissionScopes": [
      {
        "adminConsentDescription": "my admin description",
        "adminConsentDisplayName": "my admin display name",
        "id": "00000000-0000-0000-0000-000000000000",
        "isEnabled": true,
        "type": "User",
        "userConsentDescription": "my user description",
        "userConsentDisplayName": "my user display name",
        "value": "user_impersonation"
      }
    ],
    "passwordCredentials": [],
    "preferredSingleSignOnMode": null,
    "preferredTokenSigningKeyThumbprint": null,
    "replyUrls": [],
    "resourceSpecificApplicationPermissions": [],
    "samlSingleSignOnSettings": null,
    "servicePrincipalNames": [
      "00000000-0000-0000-0000-000000000000",
      "https://myURL.com"
    ],
    "servicePrincipalType": "Application",
    "signInAudience": null,
    "tags": [
      "WindowsAzureActiveDirectoryIntegratedApp"
    ],
    "tokenEncryptionKeyId": null,
    "verifiedPublisher": {
      "addedDateTime": null,
      "displayName": null,
      "verifiedPublisherId": null
    }
  }
]

--query使用 參數來擷取和儲存變數中的服務主體屬性。

# Bash script
spID=$(az ad sp list --display-name myServicePrincipalName --query "[].{spID:appId}" --output tsv)
tenantID=$(az ad sp list --display-name myServicePrincipalName --query "[].{tenant:appOwnerOrganizationId}" --output tsv)
userConsentDescr=$(az ad sp list --display-name myServicePrincipalName --query "[].{ucs:oauth2PermissionScopes.userConsentDescription[0]}" --output tsv)
echo "Using appId $spID in tenant $tenantID for $userConsentDescr"

後續步驟

既然您已瞭解如何擷取現有的服務主體,請繼續進行下一個步驟,瞭解如何管理服務主體角色。