菜鸟教程小白 发表于 2022-12-12 13:17:05

ios - 如何在 iOS 上获取 Cognito 用户池 "sub"属性


                                            <p><p>我正在使用“Cognito 用户池授权方”(无“AWS_IAM”选项,无自定义编码授权方)通过 API Gateway 调用 Lambda 方法并识别登录 iOS 客户端的用户。</p>

<p>在 Lambda 上,我使用通过 <code>event.requestContext.authorizer.claims.sub</code> 从 Cognito 用户池授权方获得的用户 ID(将用户 ID 与一些 DynamoDB 项目一起存储)。</p>

<p>我现在需要将其与 iOS 客户端中登录用户的 id 进行比较。 </p>

<p>我找到了 <code>.identityId</code>,但这(显然)返回了他 <strong>IdentityID</strong>(我可以在 Cognito 的 AWS 控制台中查找 --> Federated身份 --> 身份浏览器),<strong>与我在 Cognito 中看到的“子” id 不同</strong> --> 用户池 --> 用户和组</p>

<p>我可以通过 AWS iOS 开发工具包获取“子”吗?</p>

<p>如果我无法获取它,我应该使用什么其他 id 参数来在 Lambda 和客户端上检索以识别当前客户端用户/发出 API 请求的用户?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看来我必须通过这样的用户详细信息专门请求属性:</p>

<pre><code>AWSCognitoIdentityUserPool *pool = ;
AWSCognitoIdentityUser *user = ;

NSString *mySub;

[ continueWithBlock:^id _Nullable(AWSTask&lt;AWSCognitoIdentityUserGetDetailsResponse *&gt; * _Nonnull task) {
    if(!task.error){
      AWSCognitoIdentityUserGetDetailsResponse *response = task.result;
      NSArray&lt;AWSCognitoIdentityProviderAttributeType*&gt; *userAttributes = response.userAttributes;
      for (AWSCognitoIdentityProviderAttributeType *attr in self.userAttributes) {
            if () {
                mySub = attr.value;
            }
      }
    } else {
      NSLog(@&#34;Error fetching Cognito User Attributes: %@&#34;, task.error.localizedDescription);
    }
}];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 上获取 Cognito 用户池&#34;sub&#34;属性,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43144135/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43144135/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 上获取 Cognito 用户池 &#34;sub&#34;属性