【问题标题】:Finding a user ID, using AWS / iOS使用 AWS / iOS 查找用户 ID
【发布时间】:2019-12-30 01:28:35
【问题描述】:

我开始找到使用 AWS 的方法。 但是我有一个简单的问题,我没有通过搜索网络找到答案并尝试了我的想法。这是相关的工作代码:

override func viewDidLoad() {
    super.viewDidLoad()
    ...

    if AWSSignInManager.sharedInstance().isLoggedIn {
        print("We are already logged in!")
        // I would like to display the user identification of the current user.
    }

    ...
}

在评论的地方,我想打印当前用户的用户 ID(当有人登录时)。 我想一定有办法。我该怎么做?

【问题讨论】:

  • 是的,确实这已经是一种改进。我不完全确定这是我应该考虑的用户 ID。使用帖子中的代码,我可以获得:AccessKey、SecretKey 和 SessionKey。因为每次我启动应用程序时,这似乎都会改变。
  • 多看一下 Dharmesh 提到的链接,似乎表明我应该能够获得比 AccessKey、SecretKey 和 SessionKey 更多的东西(与我最初的想法相反)。但我想找一些更简单的东西(如果可能的话)。

标签: ios swift amazon-web-services authentication


【解决方案1】:

也许考虑 Cognito “用户名”字段,它是每个用户的唯一 ID(不可变):

        let amc = AWSMobileClient.sharedInstance();
        if (amc.isSignedIn) {
             print ("Signed in with username=\(amc.username!)")
        }

我们也可以使用getUserAttributes 来获取所有属性,如下所示:

            // See all the attributes the user has
            amc.getUserAttributes { (attributes, error) in
                if let attributes = attributes {
                    for (key, value) in attributes {
                        print("\(key): \(value)")
                    }
                } else if let error = error {
                    print("Unexpected error: \(error.localizedDescription)")
                }
            }

【讨论】:

  • 谢谢,据我所知,您建议的第一部分似乎不太有希望。在调试器下发生的情况之后,我看到了: (lldb) p amc.isSignedIn (Bool) $R10 = false (lldb) p amc.username (String?) $R12 = "myuserguy" 用户名是正确的,但令人惊讶的是出现了 isSignedIn为假。
  • 幸运的是,第二部分听起来更好。我得到这些属性: ATTRIBUTE :: phone_number_verified: false ATTRIBUTE :: email_verified: true ATTRIBUTE :: email: cwn52510@cndps.com ATTRIBUTE :: sub: a2bz6zc3-xyzt-1234-809f-51214f736a80 ATTRIBUTE :: phone_number: +839022331234 看来就像我需要的第四个属性“sub”一样。
  • 我确实可以在 AWS 控制台中为给定用户检索此号码。
  • 哦,第一部分可能是 aws sdk 中的错误:github.com/aws-amplify/aws-sdk-ios/issues/1314
【解决方案2】:
猜你喜欢
  • 2018-03-30
  • 2016-06-04
  • 2019-09-24
  • 2021-07-23
  • 2017-08-14
  • 2019-08-12
  • 2017-10-04
  • 1970-01-01
  • 2021-10-23
相关资源
最近更新 更多