ios - Amazon Cognito 使用开发人员身份验证 Obj C 设置 identityId

标签 ios objective-c amazon-web-services amazon-cognito

我正在尝试使用 Cognito 和开发人员验证身份来验证我的用户。我的问题是如何使用 objective-c 将 identityId 设置为从我的 iOS 开发人员后端返回的正确身份?

如果我在代码中手动调用或使用 postman ,我的服务器会返回正确的 ID,Cognito 会识别它并正确交换 token 。

iOS 框架似乎自行分配了一个不正确的 identityId。我真的很难理解文档,因为其中大部分内容已经过时且含糊不清。

下面是我的代码:

NSLog(@"Complete login");

    NSMutableDictionary *merge = [NSMutableDictionary dictionaryWithDictionary:self.credentialsProvider.logins];
    [merge addEntriesFromDictionary:logins];

    self.credentialsProvider.logins = merge;

    // Force a refresh of credentials to see if we need to merge
    task = [self.credentialsProvider refresh];

    NSLog(@"Complete login 2-- %@", self.credentialsProvider.identityId); //The identityId assigned is incorrect...
    NSLog(@"Complete login 2-- %@", self.credentialsProvider.identityPoolId); //The identityPoolId is correct

下面的一些清晰度: 到目前为止,它是这样工作的: 1 - 当应用程序运行时,它会自动从亚马逊分配一个随机的 identityId(因此用户最初是未经身份验证的)。 2 - 用户输入他们的凭据,然后我的代码向我的后端发出 URL 请求,该请求返回有效 token 以及用户名和密码的正确 identityId。 3 - 现在这个新的 identityId 需要在代码中以某种方式初始化以覆盖旧的未经身份验证的 identityId。 4 - 从我的服务器返回的新 idendityId 和 token 需要发送到 Cognito 进行最终身份验证,Cognito 返回更多身份验证。当我手动说我是通过 postman 发送请求时。

简而言之: 1:如何更改/设置我的 identityId? 2:如何将从客户端后端收到的更新凭据发送到 Cognito?

最佳答案

AWSCognitoCredentialsProvider 实例中的 identityId 字段具有只读属性,因此一旦初始化就无法更改。设置它的唯一方法是在它的初始化中。

id<AWSCognitoCredentialsProvider> credentialsProvider =
   [[AWSCognitoCredentialsProvider alloc]
        initWithRegionType:<Region>
          identityProvider:identityProvider
             unauthRoleArn:nil
               authRoleArn:nil];

用户通过身份验证后,请确保按如下方式更新登录映射:

credentialsProvider.logins = @{DeveloperProviderName: userIdentifier}
[credentialsProvider refresh];

来源:http://docs.aws.amazon.com/cognito/latest/developerguide/developer-authenticated-identities.html

p.s.: 确保你正确地实现了身份提供者并且在刷新方法中你应该设置 identityId

- (AWSTask *)refresh {
    /*
     * Get the identityId and token by making a call to your backend
     */
    // Call to your backend

    // Set the identity id and token
    self.identityId = response.identityId;
    self.token = response.token;
    return [AWSTask taskWithResult:self.identityId];
}

关于ios - Amazon Cognito 使用开发人员身份验证 Obj C 设置 identityId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39618356/

相关文章:

ios - React native ios 应用程序运行失败并显示消息 : reason: '-[RCTRootContentView reactTag]: unrecognized selector sent to instance

iphone - 在 iPhone 上的 sql-lite 中可以重新索引多少条记录?

ios - 返回查看 Controller 时,Core Bluetooth 不会恢复

ios - 如何保护数组字段,以便 Parse 用户可以添加对象而不允许恶意更改?

iphone - 如果特定列值为 NULL,则 SQLite 查询从表中选择列

amazon-web-services - 缺少容器属性

amazon-web-services - 如何配置 Amazon AWS Lambda 函数以防止在响应中拖尾日志?

amazon-web-services - Terraform AWS Cognito 应用程序客户端

iphone - 为 Syphon- iPhone 客户端启用 g729 编解码器

ios - 拖动的注释 View 不随 map 移动