> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth

> 使用 Microsoft Entra ID 登录和注销,并检查当前登录的用户。

`xbox auth` 管理其他命令所使用的 Microsoft Entra ID 帐户。有关登录的工作原理(包括以服务主体身份登录),请参阅
[身份验证](https://docs.xbox.com/zh-CN/tools/xbox-cli/authentication.md)。

```text theme={null}
xbox auth <command>
```

| 命令 | 说明 |
| - | - |
| `xbox auth login` | 通过系统浏览器登录,替换任何已登录的帐户。 |
| `xbox auth logout` | 注销每个已缓存的帐户。 |
| `xbox auth status` | 显示已登录的帐户。 |

所有 `auth` 命令都不接受 `--output table`。

当 `XBOX_AUTHENTICATION__MODE` 选择服务主体模式时,`auth login` 和
`auth logout` 会以 `1` 退出,而 `auth status` 会描述该服务主体。

## `auth login`

使用 Microsoft Entra ID 登录,替换任何已登录的帐户。

```bash theme={null}
xbox auth login
```

该命令会打开系统浏览器,成功后会输出帐户及其令牌的过期时间。之后的每个命令都会使用该帐户。如果缓存了多个帐户(例如由较早的 CLI 版本缓存),则在你再次运行 `auth login` 之前,命令会失败。

## `auth logout`

注销每个已缓存的帐户。

```bash theme={null}
xbox auth logout
```

该命令会输出它注销的帐户,或者说明没有已登录的帐户。使用 [`context set`](https://docs.xbox.com/zh-CN/tools/xbox-cli/commands/context) 保存的值会被保留,以同一帐户重新登录后会恢复这些值。

## `auth status`

显示已登录的帐户及其令牌。该命令可以通过 Entra ID 刷新令牌,但不会使用该令牌,因此它无法确认其他命令是否会接受该令牌。
若要查看已保存的卖家和 PlayFab 游戏,请运行 [`xbox context get`](https://docs.xbox.com/zh-CN/tools/xbox-cli/commands/context)。

```bash theme={null}
xbox auth status
```

除非你传递 `--output`,否则输出为文本。对于脚本,请请求 JSON 并检查 `signedIn`:

```bash theme={null}
xbox auth status --output json-compact
```

```json theme={null}
{"signedIn":false}
```

| 退出代码 | 含义 |
| - | - |
| `0` | 已读取状态,无论是否有帐户登录。 |
| `1` | CLI 无法读取其设置或令牌缓存,或者在服务主体模式下无法获取令牌。错误会输出到 stderr,stdout 为空。 |

### 服务主体模式

在服务主体模式下,`auth status` 会获取仅限应用的令牌并报告其声明。它从不输出令牌。

```bash theme={null}
XBOX_AUTHENTICATION__MODE=azurecli xbox auth status --output json
```

```json theme={null}
{
  "signedIn": true,
  "authenticationMode": "azurecli",
  "principalType": "app",
  "tenantId": "00000000-0000-0000-0000-000000000000",
  "appId": "11111111-1111-1111-1111-111111111111",
  "objectId": "22222222-2222-2222-2222-222222222222",
  "audience": "api://example",
  "roles": [],
  "tokenType": "Bearer",
  "tokenExpiresAt": "2030-01-01T00:00:00+00:00",
  "scopes": ["<scope>/.default"]
}
```

对于服务主体或托管标识,`principalType` 为 `app`。请参阅
[确认标识](https://docs.xbox.com/zh-CN/tools/xbox-cli/authentication.md#confirm-the-identity)。


## Related topics

- [auth_token_validated](https://docs.xbox.com/zh-CN/services/playfab/api-references/events/PlayerIdentity/auth-token-validated.md)
- [Refresh PSN Auth Token](https://docs.xbox.com/api-reference/platform-specific-methods/refresh-psn-auth-token.md)
- [Get Player Id From Auth Token](https://docs.xbox.com/api-reference/account-management/get-player-id-from-auth-token.md)
- [使用 X-token 或 OAuth 2.0 从服务中请求 User Store ID](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
- [AuthenticationProvider](https://docs.xbox.com/zh-CN/services/playfab/api-references/events/data-types/authenticationprovider.md)
