如何将 Cognito 用户池与身份池集成?

How do I integrate cognito user pool with identity pool?

我正在练习 CognitoUserPoolsSample iOS Obj-C 应用程序并尝试添加与 Cognito Identity 的集成。我已经设置了一个用户池和一个身份池,并将用户池设置为身份池的身份验证提供程序。用户池工作正常,但用户没有出现在身份池中。 这是我在 applicationDidFinishLaunchingWithOptions:

中的内容
//setup service config
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];

//Configure user pool
AWSCognitoIdentityUserPoolConfiguration *userPoolConfiguration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"CLIENT_ID"  clientSecret:@"CLIENT_SECRET" poolId:@"POOL_ID"];
[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:userPoolConfiguration forKey:@"UserPool"];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];

//configure identity pool
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionUSEast1
                                                      identityPoolId:@"IDENTITY_POOL_ID"
                                                      identityProviderManager:pool];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

到目前为止这是正确的吗?下一步是什么?为什么当我注册一个新用户时它没有出现在身份池中?身份池控制台显示创建的身份为零。

您需要将 Cognito 用户池中的令牌提供给 Cognito 联合身份服务。这正是您将 Facebook 或 Google 或任何其他提供商与联合身份服务集成的方式。

dev guide page and blog post 详细介绍此过程。

嗨这里要理解的关键是当你打电话时:

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionUSEast1
                                                      identityPoolId:@"IDENTITY_POOL_ID"
                                                      identityProviderManager:pool];

AWS 框架将为您设置一切,认知用户池和与联合身份的集成将无缝运行。

这里有一个我最初忽略的要点:http://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html

[[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
else {
       // the task result will contain the identity id
       NSString *cognitoId = task.result;
   }
return nil;
}];

这会强制从服务器刷新您的凭据。用户和会话中包含的对象可用于确认登录和关联的认知 ID,以及会话令牌。

注意不要将 MobileHubHelper 与上述代码一起使用。由于移动 HUB 助手将破坏所有这些。