

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 实现身份验证流程
<a name="authentication-implement"></a>

无论您是实现[托管登录](authentication-flows-selection-managedlogin.md)，还是使用用于身份验证的 S Amazon DK 的[自定义应用程序前端](authentication-flows-selection-sdk.md)，都必须针对要实现的身份验证类型配置应用程序客户端。以下信息介绍了如何在[应用程序客户端](user-pool-settings-client-apps.md)和应用程序中设置身份验证流程。

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

您可以在 Amazon Cognito 控制台中为应用程序客户端配置支持的流程，也可以使用软件开发工具包中的 API 来配置支持的流程。 Amazon 在完成应用程序客户端的配置以支持这些流程后，即可在您的应用程序中部署它们。

以下过程展示了如何使用 Amazon Cognito 控制台为应用程序客户端配置可用的身份验证流程。

**为应用程序客户端配置身份验证流程（使用控制台）**

1. 登录 Amazon 并导航到 [Amazon Cognito 用户池控制台](https://console.amazonaws.cn/cognito/v2/idp)。选择一个用户池或新建一个。

1. 在您的用户池配置中，选择**应用程序客户端**菜单。选择一个应用程序客户端或新建一个。

1. 在**应用程序客户端信息**下，选择**编辑**。

1. 在**应用程序客户端流程**下，选择要支持的身份验证流程。

**为身份验证流程配置应用程序客户端 (API/SDK)**  
要使用 Amazon Cognito API 为应用程序客户端配置可用的身份验证流程，请在[CreateUserPoolClient](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html#CognitoUserPools-CreateUserPoolClient-request-ExplicitAuthFlows)或[UpdateUserPoolClient](https://docs.amazonaws.cn/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  | 
| --- | --- | --- | --- | 
| [Choice-based认证](authentication-flows-selection-sdk.md#authentication-flows-selection-choice) | Server-side，客户端 | 登录时选择身份验证类型 | ALLOW\_USER\_AUTH | 
| [Sign-in 使用永久密码](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-password) | Client-side | 使用用户名和密码登录 | ALLOW\_USER\_PASSWORD\_AUTH | 
| [Sign-in 使用永久密码和安全有效载荷](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-srp) | Server-side，客户端 | 使用安全远程密码（SRP）登录 | ALLOW\_USER\_SRP\_AUTH | 
| [刷新令牌](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-refresh) | Server-side，客户端 | 从现有的经过身份验证的会话中获取新的用户令牌 | ALLOW\_REFRESH\_TOKEN\_AUTH | 
| [Server-side 身份验证](authentication-flows-public-server-side.md#amazon-cognito-user-pools-server-side-authentication-flow) | Server-side | 使用服务器端管理凭证登录 | ALLOW\_ADMIN\_USER\_PASSWORD\_AUTH | 
| [自定义身份验证](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-custom) | Server-side 以及客户端定制的应用程序。与托管登录不兼容。 | 通过 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.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#CognitoUserPools-InitiateAuth-request-AuthFlow)或[AdminInitiateAuth](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html#CognitoUserPools-AdminInitiateAuth-request-AuthFlow)请求的正文必须包含一个`AuthFlow`参数。

Choice-based 身份验证：

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

Client-based 使用 SRP 进行身份验证：

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

------