将Congnito用户池与Amazon Cognito身份集成 [英] Integrating Congnito User Pools with Amazon Cognito Identity

查看:326
本文介绍了将Congnito用户池与Amazon Cognito身份集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode(Swift)。我尝试编写代码进行注册,确认用户并登录用户到用户池。然后,成功登录后,我希望将其与Amazon Cognito Identity链接。基本上,我希望用户池中的用户数与用户数一样多。

I am using Xcode(Swift). I tried writing code that will sign up, confirm user and sign in user to the user pool. Then on successful sign in, I want it to be linked with Amazon Cognito Identity. Basically, I want to have a as many Identity Id as many users in User Pool.

到目前为止,我已经可以注册,确认用户并登录(使用显式登录)。登录成功后,我正在尝试将该用户链接到Cognito身份池,以便可以为此用户生成唯一的ID。

So far I am able to sign up, confirm user and sign in (using explicit login). On Success of Sign In, I am trying to link this user to Cognito Identity Pool so that a unique Id can be generated for this user.

当前,如果我登录到该应用程序作为其他用户,将被分配与先前用户相同的标识ID。换句话说,无论我的用户池中有多少用户,在AWS Cogntio联合身份池一侧,我只有一个身份ID。理想情况下,它应该为不同的用户创建单独的身份ID。

Currently, If I SignIn to the application as a different user it is assigned the same Identity Id as the previous user. In other words, Irrespective of how many users are there in my User Pool, On the AWS Cogntio Federated identity pool side, I have only one Identity Id. Ideally, It should have created separate Identity Id for different Users.

下面是didFinishLaunchingWithOptions函数中App委托中的代码。

Below is the code in App delegate within didFinishLaunchingWithOptions function.

let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: nil)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = serviceConfiguration
let configurationUserPool = AWSCognitoIdentityUserPoolConfiguration.init(clientId: "####", clientSecret: "####", poolId: "####")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: configurationUserPool, forKey: "testPool")
self.userPool = AWSCognitoIdentityUserPool(forKey: "testPool")

下面是Sign中的代码查看控制器。

Below is the code in Sign View controller.

@IBAction func signIn(sender: AnyObject) {

    let user = self.userPool.getUser(userName.text!)

    user.getSession(userName.text!, password: password.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {
        (task:AWSTask!) -> AnyObject! in

        if task.error != nil {
            print(task.error)
        } else {
            print("Successful Login")

            let cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
            let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: cp)

            AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                
            cp.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in

                if (task.error != nil) {
                    print("Error: ")

                } else {
                    // the task result will contain the identity id
                    print("Success with id")
                    print(task.result)
                }
                return nil
            }

            dispatch_async(dispatch_get_main_queue()){
               // do stuff here ...
            }
        }
        return nil
    })
}


推荐答案

最后,我能够纠正此问题。问题是每次我登录一个新用户时,我都在重置凭据提供程序。当我将其移至App Delegate时,它将按应有的方式开始工作。

Finally, I was able to rectify this issue. The problem was that each time I was SignIn a new user, I was resetting the credentials provider. When I moved it to the App Delegate, It started working the way it should.

这不应在signIn函数中,而应在App Delegate中。

This should not be in signIn function, rather it should be App Delegate.

let cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: cp)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                

正确的方式。

let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: nil)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = serviceConfiguration
let configurationUserPool = AWSCognitoIdentityUserPoolConfiguration.init(clientId: "####", clientSecret: "####", poolId: "####")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: configurationUserPool, forKey: "testPool")
self.userPool = AWSCognitoIdentityUserPool(forKey: "testPool")
self.cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: self.cp)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                

这篇关于将Congnito用户池与Amazon Cognito身份集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆