我一直在尝试为我的用户池用户生成此“身份ID”以访问AWS资源。但一直没有成功。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//user pool configuration
let serviceConfiguration = AWSServiceConfiguration(
region: AWSRegionType.USWest2,
credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: K.COGNITO_USER_POOL_APP_CLIENT_ID, clientSecret: K.COGNITO_USER_POOL_APP_CLIENT_SECRET, poolId: K.COGNITO_USER_POOL_ID)
//create a pool
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
self.pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
self.credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USWest2, identityPoolId: K.IDENTITY_POOL_ID, identityProviderManager: self.pool!)
let configuration = AWSServiceConfiguration(region: AWSRegionType.USWest2, credentialsProvider: self.credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
self.pool!.delegate = self
self.user = self.pool!.currentUser()
return true
}
这就是 Integrating User pool with Cognito(Swift) Documentation. 中给出的全部内容
虽然 Android 代码提到在登录映射中传递“idToken”和用户池 url,但 swift 代码中没有提到这样的事情。
到目前为止,用户登录应用程序后,我在 Xcode 的日志窗口中获取了“AccessToken”和“IdToken”。
我需要进一步做什么才能获得经过身份验证的身份?如果我做错了,请纠正我。谢谢。
最佳答案
您是对的,该文档对于身份池和用户池以及将它们集成起来并没有多大帮助。
我的建议是使用 AWS Mobile Hub,而不是依赖其他文档或示例。如果您使用该网站,它将下载一个完整的 Swift Xcode 项目,该项目可以正确集成用户池和另一个身份提供商(Google 或 Facebook)。用户池集成是移动中心的一项新功能。
此外,移动中心具有良好的下载应用程序架构,将身份管理与登录管理分开。
但是,如果您想继续使用文档来完成此任务,下面的注释集应该会有所帮助。加上以下要点。
1) 如果您使用 SDK,则无需管理代币,SDK 会为您保留和交换代币。
2) 您所要做的就是正确配置您的用户池、正确配置您的 IAM,并调用一系列简短的 SDK 调用(概述如下)。移动中心所做的一件伟大的事情就是一次为您提供所有正确的东西,让您在学习时修改和发展它。
3) 简短的调用序列是: - 建立一个用户池 - 建立一个身份池 - 使用户池成为您的服务配置的identityProviderManager (注意:您已经完成了上述所有操作,因此您只需执行以下步骤即可) - 执行获取身份 API 调用(这在您执行“获取 session ”SDK 调用时完成。 - 执行 GetCredentialsForIdentity API 调用(这在您执行“credentials”SDK 调用时完成)
届时,您将拥有凭据并能够根据您的登录(经过身份验证)状态以及 IAM 中的任何关联规则来访问 AWS 服务。
这些注释中有(我认为)有用的解释:notes on using cognito and user pools
该存储库中还有一个用户池与身份池协同工作的示例,包括注册、忘记密码等,以及进行身份合并的能力。
(Mobile Hub 中的新用户池代码现在可以很好地完成其中的一些操作(截至 4 天前添加用户池代码时),但尚未进行身份合并)。
希望这有帮助
关于ios - AWS Cognito 身份验证凭证 IOS Swift 2.3 - 3(集成用户池和身份池),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40543212/