简体   繁体   English

将 Congnito 用户池与 Amazon Cognito 身份集成

[英]Integrating Congnito User Pools with Amazon Cognito Identity

I am using Xcode(Swift).我正在使用 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.然后在成功登录后,我希望它与 Amazon Cognito Identity 相关联。 Basically, I want to have a as many Identity Id as many users in User Pool.基本上,我希望拥有与用户池中的用户一样多的身份 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.登录成功后,我尝试将此用户链接到 Cognito 身份池,以便为该用户生成唯一 ID。

Currently, If I SignIn to the application as a different user it is assigned the same Identity Id as the previous user.目前,如果我以不同的用户身份登录应用程序,它会被分配与前一个用户相同的身份 ID。 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.换句话说,无论我的用户池中有多少用户,在 AWS Cogntio 联合身份池方面,我只有一个身份 ID。 Ideally, It should have created separate Identity Id for different Users.理想情况下,它应该为不同的用户创建单独的身份 ID。

Below is the code in App delegate within didFinishLaunchingWithOptions function.下面是 didFinishLaunchingWithOptions 函数中 App 委托中的代码。

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")

Below is the code in Sign View controller.下面是 Sign View 控制器中的代码。

@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
    })
}

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.当我将它移到 App Delegate 时,它​​开始按应有的方式工作。

This should not be in signIn function, rather it should be App Delegate.这不应该在 signIn 函数中,而应该是 App Delegate。

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

Correct way.正确的方式。

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                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM