

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

# 實作身分驗證流程
<a name="authentication-implement"></a>

無論您是實作[受管登入](authentication-flows-selection-managedlogin.md)，還是使用 AWS 開發套件進行身分驗證[的自訂建置應用程式前端](authentication-flows-selection-sdk.md)，都必須為您要實作的身分驗證類型設定應用程式用戶端。下列資訊說明[應用程式用戶端](user-pool-settings-client-apps.md)和應用程式中身分驗證流程的設定。

------
#### [ App client supported flows ]

您可以在 Amazon Cognito 主控台或使用 AWS SDK 中的 API，為您的應用程式用戶端設定支援的流程。將應用程式用戶端設定為支援這些流程之後，您可以在應用程式中部署它們。

下列程序會使用 Amazon Cognito 主控台設定應用程式用戶端的可用身分驗證流程。

**為身分驗證流程設定應用程式用戶端 （主控台）**

1. 登入 AWS 並導覽至 [Amazon Cognito 使用者集區主控台](https://console.aws.amazon.com/cognito/v2/idp)。選擇使用者集區或建立新的集區。

1. 在您的使用者集區組態中，選取**應用程式用戶端**選單。選擇應用程式用戶端或建立新的用戶端。

1. 在**應用程式用戶端資訊**下，選取**編輯**。

1. 在**應用程式用戶端流程**下，選擇您要支援的身分驗證流程。

**設定應用程式用戶端進行身分驗證流程 (API/SDK)**  
若要使用 Amazon Cognito API 設定應用程式用戶端的可用身分驗證流程，請在 [CreateUserPoolClient](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html#CognitoUserPools-CreateUserPoolClient-request-ExplicitAuthFlows) 或 [UpdateUserPoolClient](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserPoolClient.html#CognitoUserPools-UpdateUserPoolClient-request-ExplicitAuthFlows) 請求`ExplicitAuthFlows`中設定 的值。以下是將安全遠端密碼 (SRP) 和選擇型身分驗證佈建給用戶端的範例。

```
"ExplicitAuthFlows": [ 
   "ALLOW_USER_AUTH",
   "ALLOW_USER_SRP_AUTH"
]
```

當您設定應用程式用戶端支援的流程時，您可以指定下列選項和 API 值。


**應用程式用戶端流程支援**  

| 身分驗證流程 | 相容性 | 主控台 | API  | 
| --- | --- | --- | --- | 
| [選擇型身分驗證](authentication-flows-selection-sdk.md#authentication-flows-selection-choice) | 伺服器端、用戶端 | 在登入時選取身分驗證類型 | ALLOW\_USER\_AUTH | 
| [使用持久性密碼登入](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-password) | 用戶端 | 使用使用者名稱和密碼登入 | ALLOW\_USER\_PASSWORD\_AUTH | 
| [使用持久性密碼和安全承載登入](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-srp) | 伺服器端、用戶端 | 使用安全的遠端密碼 (SRP) 登入 | ALLOW\_USER\_SRP\_AUTH | 
| [重新整理權杖](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-refresh) | 伺服器端、用戶端 | 從現有的已驗證工作階段取得新的使用者字符 | ALLOW\_REFRESH\_TOKEN\_AUTH | 
| [伺服器端身分驗證](authentication-flows-public-server-side.md#amazon-cognito-user-pools-server-side-authentication-flow) | 伺服器端 | 使用伺服器端管理登入資料登入 | ALLOW\_ADMIN\_USER\_PASSWORD\_AUTH | 
| [自訂身分驗證](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-custom) | 伺服器端和用戶端自訂建置的應用程式。與受管登入不相容。 | 使用來自 Lambda 觸發程序的自訂身分驗證流程登入 | ALLOW\_CUSTOM\_AUTH | 

------
#### [ Implement flows in your application ]

受管登入會自動在登入頁面中提供您設定的身分驗證選項。在自訂建置的應用程式中，使用初始流程的宣告開始身分驗證。
+ 若要為使用者選擇流程選項清單，請使用`USER_AUTH`流程宣告以[選擇為基礎的身分驗證](authentication-flows-selection-sdk.md#authentication-flows-selection-choice)。此流程具有用戶端型身分驗證流程中無法使用的可用身分驗證方法，例如[通行金鑰](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-passkey)和[無密碼](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-passwordless)身分驗證。
+ 若要選擇您的身分驗證流程，請使用應用程式[用戶端中可用的任何其他流程宣告用戶端型身分驗證](authentication-flows-selection-sdk.md#authentication-flows-selection-client)。

當您登入使用者時，[InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#CognitoUserPools-InitiateAuth-request-AuthFlow) 或 [AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html#CognitoUserPools-AdminInitiateAuth-request-AuthFlow) 請求的內文必須包含 `AuthFlow` 參數。

以選擇為基礎的身分驗證：

```
"AuthFlow": "USER_AUTH"
```

使用 SRP 的用戶端型身分驗證：

```
"AuthFlow": "USER_SRP_AUTH"
```

------