gpt4 book ai didi

swift - 将 Cognito 用户池与 Amazon Cognito 身份集成

转载 作者:行者123 更新时间:2023-11-28 06:42:46 34 4
gpt4 key购买 nike

我正在使用 Xcode(Swift)。我尝试编写代码来注册、确认用户并将用户登录到用户池。然后在成功登录后,我希望它与 Amazon Cognito Identity 相关联。基本上,我希望拥有与用户池中的用户一样多的身份 ID。

到目前为止,我能够注册、确认用户并登录(使用显式登录)。登录成功后,我尝试将该用户链接到 Cognito Identity Pool,以便为该用户生成一个唯一的 ID。

目前,如果我以不同的用户身份登录应用程序,它会被分配与前一个用户相同的身份 ID。换句话说,无论我的用户池中有多少用户,在 AWS Cogntio Federated 身份池端,我只有一个身份 ID。理想情况下,它应该为不同的用户创建单独的身份 ID。

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

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 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 时,它​​开始按应有的方式工作。

这不应该在 signIn 函数中,而应该是 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

关于swift - 将 Cognito 用户池与 Amazon Cognito 身份集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491909/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com