將未進行身分驗證使用者切換到已進行身分驗證使用者 (身分集區) - Amazon Cognito

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

將未進行身分驗證使用者切換到已進行身分驗證使用者 (身分集區)

Amazon Cognito 身分集區可支援已驗證和未驗證的使用者。未驗證的使用者即使沒有以任何身分提供者 (IdP) 路徑登入,也能存取您的 AWS 資源。這個程度的存取能在使用者登入前就顯示內容,非常有用。每個未驗證使用者在身分集區中都有專屬身分 (即使使用者尚未個別登入和驗證身分亦同)。

本節說明您的使用者選擇從未驗證身分登入,轉為使用經驗證身分登入的程序。

Android

使用者可使用未驗證的訪客身分來登入您的應用程式,最後他們可能會決定使用其中一個支援的 IdP 來登入。Amazon Cognito 確保舊身分保留與新身分相同的唯一識別符,且設定檔資料會自動合併。

設定檔合併的訊息會透過 IdentityChangedListener 界面來通知您的應用程式。請在該界面中實作 identityChanged 方法,以接收這些訊息:

@override public void identityChanged(String oldIdentityId, String newIdentityId) { // handle the change }

iOS - Objective-C

使用者可使用未驗證的訪客身分來登入您的應用程式,最後他們可能會決定使用其中一個支援的 IdP 來登入。Amazon Cognito 確保舊身分保留與新身分相同的唯一識別符,且設定檔資料會自動合併。

NSNotificationCenter 會通知您的應用程式設定檔合併的訊息:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(identityIdDidChange:) name:AWSCognitoIdentityIdChangedNotification object:nil]; -(void)identityDidChange:(NSNotification*)notification { NSDictionary *userInfo = notification.userInfo; NSLog(@"identity changed from %@ to %@", [userInfo objectForKey:AWSCognitoNotificationPreviousId], [userInfo objectForKey:AWSCognitoNotificationNewId]); }

iOS - Swift

使用者可使用未驗證的訪客身分來登入您的應用程式,最後他們可能會決定使用其中一個支援的 IdP 來登入。Amazon Cognito 確保舊身分保留與新身分相同的唯一識別符,且設定檔資料會自動合併。

NSNotificationCenter 會通知您的應用程式設定檔合併的訊息:

[NSNotificationCenter.defaultCenter().addObserver(observer: self selector:"identityDidChange" name:AWSCognitoIdentityIdChangedNotification object:nil) func identityDidChange(notification: NSNotification!) { if let userInfo = notification.userInfo as? [String: AnyObject] { print("identity changed from: \(userInfo[AWSCognitoNotificationPreviousId]) to: \(userInfo[AWSCognitoNotificationNewId])") } }

JavaScript

最初未進行身分驗證的使用者

使用者通常在一開始會使用未驗證角色。針對此角色,您設定的組態物件之登入資料屬性不需登入屬性。在這種情況下,您的預設組態可能如下所示:

// set the default config object var creds = new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'us-east-1:1699ebc0-7900-4099-b910-2df94f52a030' }); AWS.config.credentials = creds;

切換到已進行身分驗證使用者

當未驗證使用者登入 IdP,且您有一個權杖時,您可以透過呼叫自訂函數更新登入資料物件並新增登入權杖,將使用者從未驗證切換為已驗證:

// Called when an identity provider has a token for a logged in user function userLoggedIn(providerName, token) { creds.params.Logins = creds.params.Logins || {}; creds.params.Logins[providerName] = token; // Expire credentials to refresh them on the next request creds.expired = true; }

您也可建立 CognitoIdentityCredentials 物件。如果您建立此物件,您必須重設任何現有的服務物件登入資料屬性,以反映經更新的登入資料組態資訊。請參閱使用全域組態物件

如需 CognitoIdentityCredentials 物件的詳細資訊,請參閱 AWS SDK for JavaScript API 參考中的 AWS.CognitoIdentityCredentials

Unity

使用者可使用未驗證的訪客身分來登入您的應用程式,最後他們可能會決定使用其中一個支援的 IdP 來登入。Amazon Cognito 確保舊身分保留與新身分相同的唯一識別符,且設定檔資料會自動合併。

您可以訂閱 IdentityChangedEvent,以接收設定檔合併的訊息:

credentialsProvider.IdentityChangedEvent += delegate(object sender, CognitoAWSCredentials.IdentityChangedArgs e) { // handle the change Debug.log("Identity changed from " + e.OldIdentityId + " to " + e.NewIdentityId); };

Xamarin

使用者可使用未驗證的訪客身分來登入您的應用程式,最後他們可能會決定使用其中一個支援的 IdP 來登入。Amazon Cognito 確保舊身分保留與新身分相同的唯一識別符,且設定檔資料會自動合併。

credentialsProvider.IdentityChangedEvent += delegate(object sender, CognitoAWSCredentials.IdentityChangedArgs e){ // handle the change Console.WriteLine("Identity changed from " + e.OldIdentityId + " to " + e.NewIdentityId); };