如何 link Cognito Identity ID with User Attributes?
How to link Cognito Identity ID with User Attributes?
我正在尝试为 iOS 编写的应用程序创建用户登录系统 Swift 3. 使用下面的代码,我在我的用户池中为用户获取了唯一的 Cognito 身份 ID,但我我不确定如何处理此 ID。我可以 link 它给用户并获得与该用户关联的属性吗?
代码:
@IBAction func loginPressed(_ sender: Any) {
user = self.pool!.getUser(usernameTextField.text!)
user?.getSession(usernameTextField.text!, password: passwordTextField.text!, validationData: nil).continue({ task in
if let err = task.error { // some sort of error
print("LOGIN FAILED")
print(err)
//print(err.userInfo["message"] as! String)
}
else { //Successful login!
// this gets our token from the User Pool
let ret = task.result! as AWSCognitoIdentityUserSession
let myToken = ret.idToken?.tokenString;
print("Token: ", myToken);
let customcedentialProvider = AWSCustomIdentityProvider(tokens: [AWSCustomIdentityProvider.CognitoTokenKey : myToken!])
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoConstants.COGNITO_REGIONTYPE, identityPoolId: CognitoConstants.COGNITO_IDENTITY_POOL_ID, identityProviderManager: customcedentialProvider)
let configuration = AWSServiceConfiguration(region: CognitoConstants.COGNITO_REGIONTYPE, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
// wipe cached creds
credentialsProvider.clearKeychain()
credentialsProvider.clearCredentials()
// hit it
credentialsProvider.getIdentityId().continue({ (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
print("Error: ")
} else {
// the task result will contain the identity id
let UserIdentityID = task.result as String? // Im saving user identity id in constant variable called "kUserIdentityID"
print(UserIdentityID)// my identityID
print(credentialsProvider.identityId)// my identityID
}
return nil
})
}
return nil
})
}
输出(身份 ID):
Optional("us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
谢谢!
Cognito UserPool 是一个 "identity provider",如 Facebook,Google 等。而 Cognito Federated Identity 有助于将不同登录名之间的映射保持为一个唯一 ID,通常称为 "Cognito Identity ID" .通常,您在 Cognito 联合身份下的数据集中存储和检索数据。并同步多个登录。如果我理解您的问题,您可能希望将用户池 "username" 存储在上述数据集中,以便您可以检索它并查询用户池
self.pool 指的是您的 Cognito 用户池。用户池至少有一个用户名和密码,但通常还有其他属性,您可以使用 user.getDetails 获取这些属性。用户池是 IdP(身份提供者)。
identityId 是一个 Cognito Identity 概念,其主要目的是为来自 IdP 的一个或多个身份提供唯一 ID。
不用担心被混淆。 Cognito 非常令人困惑,我发现它非常令人困惑,所以我根据我的笔记写了一个小的 powerpoint 演示文稿。这是一个 link 的图表,应该对你有帮助。Diagram of Cognito
另外,我建议您使用 AWS Mobile Hub Helper 作为起点。它将下载 swift 代码示例应用程序。并且示例代码使用了 aws-mobilehub-helper-ios 包装器,简化了很多 SDK 并使其更加合理。下载的代码是Swift2但是在swift3.
中获取运行并不难
我正在尝试为 iOS 编写的应用程序创建用户登录系统 Swift 3. 使用下面的代码,我在我的用户池中为用户获取了唯一的 Cognito 身份 ID,但我我不确定如何处理此 ID。我可以 link 它给用户并获得与该用户关联的属性吗?
代码:
@IBAction func loginPressed(_ sender: Any) {
user = self.pool!.getUser(usernameTextField.text!)
user?.getSession(usernameTextField.text!, password: passwordTextField.text!, validationData: nil).continue({ task in
if let err = task.error { // some sort of error
print("LOGIN FAILED")
print(err)
//print(err.userInfo["message"] as! String)
}
else { //Successful login!
// this gets our token from the User Pool
let ret = task.result! as AWSCognitoIdentityUserSession
let myToken = ret.idToken?.tokenString;
print("Token: ", myToken);
let customcedentialProvider = AWSCustomIdentityProvider(tokens: [AWSCustomIdentityProvider.CognitoTokenKey : myToken!])
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoConstants.COGNITO_REGIONTYPE, identityPoolId: CognitoConstants.COGNITO_IDENTITY_POOL_ID, identityProviderManager: customcedentialProvider)
let configuration = AWSServiceConfiguration(region: CognitoConstants.COGNITO_REGIONTYPE, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
// wipe cached creds
credentialsProvider.clearKeychain()
credentialsProvider.clearCredentials()
// hit it
credentialsProvider.getIdentityId().continue({ (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
print("Error: ")
} else {
// the task result will contain the identity id
let UserIdentityID = task.result as String? // Im saving user identity id in constant variable called "kUserIdentityID"
print(UserIdentityID)// my identityID
print(credentialsProvider.identityId)// my identityID
}
return nil
})
}
return nil
})
}
输出(身份 ID):
Optional("us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
谢谢!
Cognito UserPool 是一个 "identity provider",如 Facebook,Google 等。而 Cognito Federated Identity 有助于将不同登录名之间的映射保持为一个唯一 ID,通常称为 "Cognito Identity ID" .通常,您在 Cognito 联合身份下的数据集中存储和检索数据。并同步多个登录。如果我理解您的问题,您可能希望将用户池 "username" 存储在上述数据集中,以便您可以检索它并查询用户池
self.pool 指的是您的 Cognito 用户池。用户池至少有一个用户名和密码,但通常还有其他属性,您可以使用 user.getDetails 获取这些属性。用户池是 IdP(身份提供者)。
identityId 是一个 Cognito Identity 概念,其主要目的是为来自 IdP 的一个或多个身份提供唯一 ID。
不用担心被混淆。 Cognito 非常令人困惑,我发现它非常令人困惑,所以我根据我的笔记写了一个小的 powerpoint 演示文稿。这是一个 link 的图表,应该对你有帮助。Diagram of Cognito
另外,我建议您使用 AWS Mobile Hub Helper 作为起点。它将下载 swift 代码示例应用程序。并且示例代码使用了 aws-mobilehub-helper-ios 包装器,简化了很多 SDK 并使其更加合理。下载的代码是Swift2但是在swift3.
中获取运行并不难