以使用者集區進行身分驗證 - Amazon Cognito

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

以使用者集區進行身分驗證

您的應用程式使用者可以直接透過使用者集區登入,也可以透過第三方身分識別提供者 (IdP) 進行聯合。用戶池管理處理通過 Facebook,谷歌,Amazon 和蘋果以及 OpenID Connect(OIDC)和 SAML 從社交登錄返回的令牌的開銷。 IdPs

身分驗證成功之後,Amazon Cognito 會將使用者集區權杖傳回至您的應用程式。您可以使用這些權杖,授予使用者存取您自己的伺服器端資源或 Amazon API Gateway 的權限。或者,您可以將它們交換為 AWS 憑據以訪問其他 AWS 服務。

身分驗證概觀

Web 和行動應用程式的使用者集區權杖處理和管理任務,就是在用戶端上透過 Amazon Cognito 軟體開發套件所提供。同樣地,如果這時存在有效 (未過期) 重新整理權杖,而且 ID 權杖和存取權杖的有效性至少還剩餘 5 分鐘,Mobile SDK for iOS 和 Mobile SDK for Android 就會自動重新整理您的 ID 權杖和存取權杖。如需有關開發套件的資訊,以及 Android 和 iOS 的範例程式碼 JavaScript,請參閱 Amazon Cognito 使用者集區開發套件。

當您的應用程式使用者登入成功之後,Amazon Cognito 就會為該驗證成功的使用者建立工作階段,並且傳回 ID、存取權限和重新整理權杖。

JavaScript
// Amazon Cognito creates a session which includes the id, access, and refresh tokens of an authenticated user. var authenticationData = { Username : 'username', Password : 'password', }; var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData); var poolData = { UserPoolId : 'us-east-1_ExaMPle', ClientId : '1example23456789' }; var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); var userData = { Username : 'username', Pool : userPool }; var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData); cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { var accessToken = result.getAccessToken().getJwtToken(); /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */ var idToken = result.idToken.jwtToken; }, onFailure: function(err) { alert(err); }, });
Android
// Session is an object of the type CognitoUserSession, and includes the id, access, and refresh tokens for a user. String idToken = session.getIdToken().getJWTToken(); String accessToken = session.getAccessToken().getJWT();
iOS - swift
// AWSCognitoIdentityUserSession includes id, access, and refresh tokens for a user. - (AWSTask<AWSCognitoIdentityUserSession *> *)getSession;
iOS - objective-C
// AWSCognitoIdentityUserSession includes the id, access, and refresh tokens for a user. [[user getSession:@"username" password:@"password" validationData:nil scopes:nil] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) { // success, task.result has user session return nil; }];