

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

# 將 Amazon Cognito 身分驗證和授權與 Web 和行動應用程式整合
<a name="cognito-integrate-apps"></a>

Amazon Cognito 的實作是 AWS 管理主控台 或 AWS SDK 管理工具和應用程式中 SDK 程式庫的混合。Amazon Cognito 主控台是用於設定和管理 Amazon Cognito 使用者集區和身分集區的視覺化介面。

您可以使用 Amazon Cognito 使用者集區建立的最低努力整合是使用[受管登入](cognito-terms.md#terms-managedlogin)。受管登入是ready-to-use Web 型登入應用程式，用於快速測試和部署 Amazon Cognito 使用者集區。使用受管登入的使用者集區身分驗證需要 OpenID Connect (OIDC) 程式庫，以引導使用者前往託管登入頁面。在此一系列的使用者互動和重新導向 Web 端點中，Amazon Cognito 會處理身分驗證流程，包括第三方登入、多重要素驗證 (MFA)，並選擇身分驗證流程。您的應用程式只需要處理 Amazon Cognito 在回應中傳回的身分驗證結果。

您也可以將 AWS SDK 新增至應用程式、自訂建置身分驗證介面，以及叫用 API 操作來驗證和授權使用者。 [AWS Amplify](https://docs.amplify.aws/)是 AWS 服務 用於建置完整堆疊應用程式的 ，後端為 Amazon Cognito 身分驗證。

例如，您的應用程式可能會叫用受管登入以進行使用者登入，然後從您的應用程式碼呼叫權杖端點，以交換使用者的權杖授權碼。然後，您的應用程式必須解譯和儲存使用者權杖，並在適當情況下出示權杖以進行驗證和授權。Amplify 針對這些程序新增了具有內建功能的引導式整合工具。

您也可以完全使用程式碼建置 Amazon Cognito 資源。身分集區沒有與使用者集區相同的受管身分驗證選項，若要存取應用程式中的 AWS 登入資料，請在匯入的 SDK 模組中實作身分集區操作。若要開始使用您自己的自訂應用程式程式碼，請造訪 [AWS SDKs](https://aws.amazon.com/developer/tools/) 的 Amazon Cognito [程式碼範例](https://docs.aws.amazon.com/cognito/latest/developerguide/service_code_examples.html)。若要作為 OpenID Connect 身分提供者與 Amazon Cognito 整合，請使用 [OpenID Connect 開發人員工具](https://openid.net/certified-open-id-developer-tools/)。

在使用 Amazon Cognito 身分驗證和授權之前，請選擇一個應用程式平台並準備好要與服務進行整合的程式碼。如需可用的平台 AWS SDKs，請參閱 [使用 AWS SDKs身分驗證](#amazon-cognito-authentication-with-sdks)。 AWS CLI 是適用於 Amazon Cognito 和其他 的命令列 SDK AWS 服務，是開始熟悉 Amazon Cognito API 操作及其語法的寶貴位置。

**注意**  
有些 Amazon Cognito 元件只能使用 API 進行設定。例如，您只能透過使用請求來更新 [CreateUserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPool.html) 或 [UpdateUserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserPool.html) API 請求中 [UserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UserPoolType.html) 類別的 `LambdaConfig` 屬性，藉此設定使用者集區[自訂簡訊或電子郵件寄件者](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-sender-triggers.html) Lambda 觸發程序。

Amazon Cognito 使用者集區 API 會與多種類別的 API 操作共用其命名空間。有一個類別用於設定使用者集區及其程序、身分提供者和使用者。另一個類別包括未經驗證的操作，可讓公有用戶端中的使用者登入、登出和管理其設定檔。最終類別的 API 操作會執行您在機密伺服器端用戶端使用自己的 AWS 登入資料授權的使用者操作。在您開始實作應用程式程式碼之前，必須先知道預定的應用程式架構。如需詳細資訊，請參閱[了解 API、OIDC 和受管登入頁面身分驗證](authentication-flows-public-server-side.md#user-pools-API-operations)。

**Topics**
+ [使用 進行身分驗證 AWS Amplify](#cognito-integrate-apps-amplify)
+ [使用 AWS SDKs身分驗證](#amazon-cognito-authentication-with-sdks)
+ [身分驗證如何與 Amazon Cognito 搭配使用](cognito-how-to-authenticate.md)
+ [搭配 AWS SDK 使用此服務](sdk-general-information-section.md)
+ [使用 Amazon Verified Permissions 進行授權](amazon-cognito-authorization-with-avp.md)

## 使用 進行身分驗證 AWS Amplify
<a name="cognito-integrate-apps-amplify"></a>

AWS Amplify 是建置 Web 和行動應用程式的完整解決方案。有了 Amplify，您就可以使用 Amplify 程式庫連線到現有資源，也可以使用 Amplify 命令列介面 (CLI) 建立和設定新資源。Amplify 也有連線 UI 元件，如 [Authenticator](https://ui.docs.amplify.aws/react/connected-components/authenticator)，可用於設定和自訂您應用程式中的登入和註冊體驗。

若要在前端應用程式中使用 Amplify 驗證功能，請參閱下列各平台的文件。
+ [React 的 Amplify 身分驗證](https://docs.amplify.aws/react/start/)
+ [React Native 的 Amplify 身分驗證](https://docs.amplify.aws/react-native/start/)
+ [Swift (iOS) 的 Amplify 身分驗證](https://docs.amplify.aws/swift/start/)
+ [Android 的 Amplify 身分驗證](https://docs.amplify.aws/android/start/)
+ [Flutter 的 Amplify 身分驗證](https://docs.amplify.aws/flutter/start/)

Amplify 程式庫是開放原始碼，可在 [GitHub](https://github.com/aws-amplify) 上取得。若要深入了解 Amplify Auth 如何實作 Amazon Cognito 驗證，請造訪下列程式庫。
+ [amplify-js](https://github.com/aws-amplify/amplify-js/tree/main/packages/auth)
+ [amplify-swift](https://github.com/aws-amplify/amplify-swift/tree/main/Amplify/Categories/Auth)
+ [amplify-flutter](https://github.com/aws-amplify/amplify-flutter/tree/main/packages/auth)
+ [amplify-android](https://github.com/aws-amplify/amplify-android/tree/main/aws-auth-cognito)

### 使用 Amplify 建立使用者介面 (UI)
<a name="cognito-integrate-apps-amplify-ui"></a>

[使用者集區受管登入](cognito-user-pools-managed-login.md) 可以滿足 Web 或行動應用程式身分驗證前端的基本需求。若要在受管登入可容納的參數之外自訂您的使用者介面 (UI)，請自訂建置應用程式。[Amplify UI](https://ui.docs.amplify.aws/) 是各種不同語言的可自訂前端元件的集合。

![範例 Amplify Authenticator 應用程式的螢幕擷取畫面。](http://docs.aws.amazon.com/zh_tw/cognito/latest/developerguide/images/amplify-authenticator-ui.png)


若要開始使用您的自訂驗證元件，請造訪下列 Authenticator 元件的文件。
+ [Authenticator for Android](https://ui.docs.amplify.aws/android/connected-components/authenticator)
+ [Authenticator for Angular](https://ui.docs.amplify.aws/angular/connected-components/authenticator)
+ [Authenticator for Flutter](https://ui.docs.amplify.aws/flutter/connected-components/authenticator)
+ [Authenticator for React](https://ui.docs.amplify.aws/react/connected-components/authenticator)
+ [Authenticator for React Native](https://ui.docs.amplify.aws/react-native/connected-components/authenticator)
+ [Authenticator for Swift](https://ui.docs.amplify.aws/swift/connected-components/authenticator)
+ [Authenticator for Vue](https://ui.docs.amplify.aws/vue/connected-components/authenticator)

## 使用 AWS SDKs身分驗證
<a name="amazon-cognito-authentication-with-sdks"></a>

若要使用安全的後端來建置與 Amazon Cognito 互動的您自己的身分微型服務，請使用您選擇的語言 AWS SDK 連線至 Amazon Cognito 使用者集區和 Amazon Cognito 身分集區 API。

如需各個 API 操作的詳細內容，請參閱 [Amazon Cognito 使用者集區 API 參考](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_Operations.html)與 [Amazon Cognito API 參考](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/Welcome.html)。這些文件包含[https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#API_InitiateAuth_SeeAlso](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#API_InitiateAuth_SeeAlso)部分，其中包含可在支援平台中使用各種 SDK 的資源。
+ [AWS 命令列介面](https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/index.html#cli-aws-cognito-idp)
+ [AWS 適用於 .NET 的 SDK](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/CognitoIdentityProvider/TCognitoIdentityProviderClient.html)
+ [AWS 適用於 C\+\+ 的 SDK](https://sdk.amazonaws.com/cpp/api/LATEST/aws-cpp-sdk-cognito-idp/html/class_aws_1_1_cognito_identity_provider_1_1_cognito_identity_provider_client.html)
+ [AWS 適用於 Go 的 SDK](https://docs.aws.amazon.com/sdk-for-go/api/service/cognitoidentityprovider/#CognitoIdentityProvider)
+ [AWS 適用於 Java V2 的 SDK](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cognitoidentityprovider/CognitoIdentityProviderClient.html)
+ [AWS 適用於 JavaScript 的 SDK](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html)
+ [AWS 適用於 PHP V3 的 SDK](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.CognitoIdentityProvider.CognitoIdentityProviderClient.html)
+ [AWS 適用於 Python 的 SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp.html)
+ [AWS 適用於 Ruby V3 的 SDK](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CognitoIdentityProvider/Client.html)