AWS - 为相同的 Cognito 身份 ID 添加身份提供者

AWS - Add identity provider for same Cognito Identity ID

我正在使用 AWS SDK,将联合身份提供商与 Cognito 结合使用。现在,我正在这样做:

private void SetupCognitoStuff()
{
    _cognitoCredentials = new CognitoAWSCredentials(
        MY_IDENTITY ID, // Identity Pool ID
        _awsRegion); // Region

    if (_identityProviderName != null)
        _cognitoCredentials.AddLogin(_identityProviderName, _identityProviderToken);

    _identityId = GetIdentityId();
}

这可以很好地创建或检索用户的凭据,使用 Facebook 作为身份提供者。我还在应用程序的设置中缓存了 Cognito Identity ID。

那么,现在假设用户下次使用我的应用程序时,他们选择了不同的登录提供商(假设 Google)。我已经缓存了他们上次登录(通过 Facebook)时的 Cognito 身份 ID。这次我实例化 CognitoAWSCredentials 时,如何告诉它我想使用现有的 Cognito 身份 ID,并且应该添加 Google 作为第二个身份提供者,而不是创建一个全新的 Cognito 身份?

the documentation for the raw API,应该可以:

Merging Identities

If you pass in a token for a login that is not currently linked to the given identity, but is linked to another identity, the two identities are merged. Once merged, one identity becomes the parent/owner of all associated logins and the other is disabled. In this case, the identity ID of the parent/owner is returned. You are expected to update your local cache if this value differs (this is handled for you if you are using the providers in the AWS Mobile SDKs or AWS SDK for JavaScript in the Browser).

因此,如果是这种情况,那么它如何知道(即我如何告诉它)在使用不同的身份提供者调用我的上述函数时要使用的现有身份 ID?

this page 开始,看起来可以通过原始 API 调用 GetCredentialsForIdentity 并在 "IdentityId" 字段中传入现有身份 ID 和新身份提供者来完成"Logins" 字段中的信息:

Request Syntax

{

"CustomRoleArn": "string",

"IdentityId": "string",

"Logins": 

    {

        "string" :

            "string"

    } 
}

我只是不确定如何使用 CognitoAWSCredentials class.

将其转换为 SDK

通过提供者 1 进行身份验证后,使用提供者 2 令牌更新凭据对象的登录映射。 您需要更新凭据对象的 Logins 映射以包含 Google 的映射。你可以弄清楚它是如何为你的 sdk 完成的。例如。 javascript,你可以

AWS.config.credentials.params.Logins['accounts.google.com'] = googleToken;

Javascript reference