

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 管理及搜尋使用者帳戶
<a name="how-to-manage-user-accounts"></a>

使用者集區可以包含數百萬使用者。使用此大小的資料集對管理員來說是一項挑戰。Amazon Cognito 具有尋找和修改使用者設定檔的工具。尋找使用者的首要方法是 Amazon Cognito 主控台**的使用者**功能表，以及 [ListUsers](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html)。在擷取使用者相關資訊的方法中，這些選項不會影響成本，例如 [AdminGetUser](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminGetUser.html)。

本指南的本節提供有關在使用者集區中尋找和更新使用者設定檔的資訊。

## 檢視使用者屬性
<a name="manage-user-accounts-viewing-user-attributes"></a>

使用下列處理程序，在 Amazon Cognito 主控台中檢視使用者屬性。

**檢視使用者屬性**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**選單，然後在清單中選取使用者。

1. 在使用者詳細資訊頁面，**User attributes** (使用者屬性) 下，您可以檢視與使用者相關聯的屬性。

## 重設使用者密碼
<a name="manage-user-accounts-reset-user-password"></a>

使用下列處理程序，在 Amazon Cognito 主控台中重設使用者密碼。

**重設使用者密碼**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**選單，然後在清單中選取使用者。

1. 在使用者詳細資訊頁面上，選擇 **Actions** (動作)、**Reset password** (重設密碼)。

1. 在 **Reset password** (重設密碼) 對話方塊中檢閱資訊，準備好時，選擇 **Reset** (重設)。

   此動作會導致立即傳送確認碼給使用者，並將使用者狀態變更為 `RESET_REQUIRED`，以停用使用者目前的密碼。**Reset password** (重設密碼) 代碼有效期為 1 小時。

## 啟用、停用和刪除使用者帳戶
<a name="manage-user-accounts-enable-disable"></a>

您可以刪除未使用的使用者設定檔，或者，如果您想要暫時防止存取，請停用它們。使用者可以刪除自己的帳戶，但只有使用者集區管理員才能啟用和停用使用者帳戶。

**刪除的影響**  
使用者無法使用已刪除的使用者帳戶登入，若要重新取得存取權， 必須註冊或再次建立。

**停用帳戶的影響**  
當您停用使用者帳戶時，Amazon Cognito 會自動使所有已驗證的工作階段失效、停用使用者帳戶以進行登入，以及[撤銷其存取權和重新整理權杖](token-revocation.md)。當使用者嘗試登入您停用的帳戶`User is not enabled`時，Amazon Cognito 會傳回包含訊息的`invalid_request`錯誤。此行為不會隨著應用程式用戶端[的使用者存在揭露設定](cognito-user-pool-managing-errors.md)而變更。您可以停用本機使用者帳戶和聯合使用者帳戶的本機設定檔。當使用者使用受管登入或傳統託管 UI 登入時，您會停用其帳戶，然後嘗試使用維護其已驗證工作階段的瀏覽器 Cookie 再次登入時，Amazon Cognito 會將他們重新導向至登入頁面。

**啟用帳戶的效果**  
啟用帳戶之後，使用者可以立即登入帳戶。使用者帳戶預設為啟用。使用者的屬性和密碼與其帳戶停用前保持不變。無論您已停用使用者帳戶或分別撤銷重新整理權杖，應用程式撤銷的權杖都會在您啟用擁有權杖的使用者帳戶後保持無效。

------
#### [ Delete a user account (console) ]

**刪除使用者帳戶**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**功能表，然後選取清單中使用者使用者名稱旁的選項按鈕。

1. 選擇 **刪除**。

1. 選擇**停用使用者存取**。

1. 選擇 **刪除**。

------
#### [ Delete a user account (API) ]

使用者可以使用自助access-token-authorized [DeleteUser](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_DeleteUser.html) API 操作來刪除其帳戶。以下是`DeleteUser`請求內文的範例。

```
{
   "AccessToken": "{{eyJra456defEXAMPLE}}"
}
```

管理員可以使用 IAM 授權的 [AdminDeleteUser](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html) API 操作刪除使用者帳戶。以下是`AdminDeleteUser`請求內文的範例。

```
{
   "Username": "{{testuser}}",
   "UserPoolId": "{{us-west-2_EXAMPLE}}"
}
```

------
#### [ Disable a user account (console) ]

**停用使用者帳戶**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**選單，然後在清單中選取使用者的使用者名稱。

1. 在使用者詳細資訊頁面上，選擇**動作**、**停用使用者存取**。

1. 在此建立的對話方塊中，選擇**停用**。

------
#### [ Disable a user account (API) ]

管理員可以使用 IAM 授權 [AdminDisableUser](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDisableUser.html) API 操作停用使用者帳戶。以下是`AdminDisableUser`請求內文的範例。

```
{
   "Username": "{{testuser}}",
   "UserPoolId": "{{us-west-2_EXAMPLE}}"
}
```

------
#### [ Enable a user account (console) ]

**啟用使用者帳戶**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**選單，然後在清單中選取使用者的使用者名稱。

1. 在使用者詳細資訊頁面上，選擇**動作**、**啟用使用者存取**。

1. 在此建立的對話方塊中，選擇**啟用**。

------
#### [ Enable a user account (API) ]

管理員可以使用 IAM 授權的 [AdminEnableUser](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminEnableUser.html) API 操作啟用使用者帳戶。以下是`AdminEnableUser`請求內文的範例。

```
{
   "Username": "{{testuser}}",
   "UserPoolId": "{{us-west-2_EXAMPLE}}"
}
```

------

## 搜尋使用者屬性
<a name="manage-user-accounts-searching-user-attributes"></a>

如果您已經建立使用者集區，即可從 **中的 **Users AWS 管理主控台(使用者) 面板進行搜尋。您也可以使用 Amazon Cognito [ListUsers API](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html)，其接受 **Filter** 參數。

您可以搜尋下列任何標準屬性。無法搜尋自訂屬性。
+ username (區分大小寫)
+ email
+ phone\_number
+ name
+ given\_name
+ family\_name
+ preferred\_username
+ cognito:user\_status (在主控台中稱為 **Status (狀態)**、區分大小寫)
+ status (在主控台中稱為 **Enabled (已啟用)**、區分大小寫)
+ sub

**注意**  
您也可以使用用戶端篩選條件列出使用者。與伺服器端篩選條件相符的屬性不超過 1 個。若要執行進階搜尋，請將用戶端篩選條件與 AWS Command Line Interface中的 `list-users` 動作 `--query` 參數搭配使用。當您使用用戶端篩選條件時，ListUsers 會傳回零個或多個使用者的分頁清單。您可以連續接收多個結果為零的頁面。使用傳回的每個分頁權杖重複查詢，直到您收到空分頁權杖值為止，然後檢閱合併的結果。  
如需伺服器端和用戶端篩選的詳細資訊，請參閱 AWS Command Line Interface 《 使用者指南》中的[篩選 AWS CLI 輸出](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html)。

## 使用 搜尋使用者 AWS 管理主控台
<a name="cognito-user-pools-manage-user-accounts-searching-for-users-using-console"></a>

如果您已經建立使用者集區，即可從 **中的 **Users AWS 管理主控台(使用者) 面板進行搜尋。

AWS 管理主控台 搜尋一律是字首 (「開頭」) 搜尋。

**在 Amazon Cognito 主控台中搜尋使用者**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。系統可能會提示您輸入 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區。

1. 選擇**使用者**選單，然後在搜尋欄位中輸入使用者名稱。請注意，某些屬性值須區分大小寫 (例如 **Username** (使用者名稱))。

   您也可以透過調整搜尋篩選條件來尋找使用者，將範圍縮小至其他使用者屬性，例如 **Email** (電子郵件)、**Phone number** (電話號碼) 或 **Last name** (姓氏)。

## 使用 `ListUsers` API 來搜尋使用者
<a name="cognito-user-pools-searching-for-users-using-listusers-api"></a>

 若要從您的應用程式搜尋使用者，請使用 Amazon Cognito [ListUsers API](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html)。此 API 會使用下列參數：
+  `AttributesToGet`：任何字串陣列，其中每個字串都是要在搜尋結果中，為每個使用者傳回的使用者屬性名稱。若要擷取所有屬性，請勿包含 `AttributesToGet` 參數，或請求其值為常值字串 `null` 的 `AttributesToGet`。
+  `Filter`：格式為「`AttributeName` `Filter-Type` "`AttributeValue`"」的篩選字串。篩選字串中的引號必須使用斜線 (`\`) 字元來逸出。例如 `"family_name = \"Reddy\""`。如果篩選字串是空的，`ListUsers`會傳回使用者集區中的所有使用者。
  +  `AttributeName`：所要搜尋的屬性名稱。您一次只能搜尋一個屬性。
**注意**  
您只能搜尋標準屬性。無法搜尋自訂屬性。這是因為只有已編製索引的屬性可供搜尋，而自訂屬性無法編製索引。
  +  `Filter-Type`：若要搜尋完全相符的項目，請使用 `=`，例如，`given_name = "Jon"`。若要搜尋字首 (「開頭為」) 相符的項目，請使用 `^=`，例如，`given_name ^= "Jon"`。
  +  `AttributeValue`：必須符合為每個使用者的屬性值。
+  `Limit`：所要傳回的使用者數量上限。
+  `PaginationToken`：此權杖可用來從先前的搜尋取得更多結果。Amazon Cognito 將在一小時後讓分頁權杖過期。
+  `UserPoolId`：要對其執行搜尋之使用者集區的使用者集區 ID。

所有搜尋皆區分大小寫。搜尋結果會依 `AttributeName`字串指定的屬性遞增排序。

## 使用 `ListUsers` API 的範例
<a name="cognito-user-pools-searching-for-users-listusers-api-examples"></a>

下列範例會傳回所有使用者，並包含所有屬性。

```
{
    "AttributesToGet": null,
    "Filter": "",
    "Limit": 10,
    "UserPoolId": "us-east-1_samplepool"
}
```

下列範例會傳回電話號碼以 "\+1312" 開頭的所有使用者，並包含所有屬性。

```
{
    "AttributesToGet": null,
    "Filter": "phone_number ^= \"+1312\"",
    "Limit": 10,
    "UserPoolId": "us-east-1_samplepool"
}
```

下列範例會傳回姓氏為 "Reddy" 的前 10 個使用者。針對每個使用者，搜尋結果都會包含使用者的名字、電話號碼和電子郵件地址。如果使用者集區中有超過 10 個相符的使用者，則回應會包含分頁權杖。

```
{
    "AttributesToGet": [
        "given_name", 
        "phone_number", 
        "email"
    ],
    "Filter": "family_name = \"Reddy\"",
    "Limit": 10,
    "UserPoolId": "us-east-1_samplepool"
}
```

如果上述範例傳回分頁權杖，則下列範例會傳回下 10 個符合相同篩選字串的使用者。

```
{
    "AttributesToGet": [
        "given_name", 
        "phone_number", 
        "email"
    ],
    "Filter": "family_name = \"Reddy\"",
    "Limit": 10,
    "PaginationToken": "pagination_token_from_previous_search",
    "UserPoolId": "us-east-1_samplepool"
}
```