你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

获取现有服务主体

列出服务主体

如果已有要使用的现有服务主体,此步骤说明如何检索现有服务主体。

可以使用 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 Key Vault 中证书的机密,但默认情况下不会存储其他机密。 如果忘记了身份验证方法或机密, 请重置服务主体凭据

服务主体属性

获取使用 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"

后续步骤

现在您已经学会如何检索现有的服务主体,接下来请学习如何管理服务主体的角色。