【问题标题】:AWS / iOS / Cognito: unauthenticated access is not supported for this identity poolAWS / iOS / Cognito:此身份池不支持未经身份验证的访问
【发布时间】:2017-05-31 14:43:53
【问题描述】:

我正在尝试让 S3TransferUtilitySampleObjC 项目使用我的 Cognito 和 AWS 凭证。

不幸的是,我收到以下错误:

AWSURLSessionManager.m line:566 | -[AWSURLSessionManager printHTTPHeadersForResponse:] | Response headers:
{
    Connection = "keep-alive";
    "Content-Length" = 111;
    "Content-Type" = "application/x-amz-json-1.1";
    Date = "Mon, 16 Jan 2017 17:17:50 GMT";
    "x-amzn-ErrorMessage" = "Unauthenticated access is not supported for this identity pool.";
    "x-amzn-ErrorType" = "NotAuthorizedException:";
    "x-amzn-RequestId" = "xxxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxx";
}

你能帮忙吗?


代码(请注意,我只从 SecondViewController 中删除了一些代码并修改了常量中的值 - 我还修改了 Info.plist 文件(见下面的屏幕截图)

我目前只专注于从我的 S3 存储桶下载文件。 因此我只修改了以下内容:

// In Constants.m
NSString *const S3BucketName = @"mybucketname";
NSString *const S3DownloadKeyName = @"config.plist";

// In SecondViewController.m

#import "SecondViewController.h"
#import "AppDelegate.h"
#import <AWSS3/AWSS3.h>
#import "Constants.h"

@interface SecondViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@property (copy, nonatomic) AWSS3TransferUtilityDownloadCompletionHandlerBlock completionHandler;
@property (copy, nonatomic) AWSS3TransferUtilityProgressBlock progressBlock;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.progressView.progress = 0;
    self.statusLabel.text = @"Ready";

    __weak SecondViewController *weakSelf = self;
    self.completionHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {
                weakSelf.statusLabel.text = @"Failed to Download";
            }
            if (data) {
                weakSelf.statusLabel.text = @"Successfully Downloaded";
                weakSelf.imageView.image = [UIImage imageWithData:data];
                weakSelf.progressView.progress = 1.0;
            }
        });
    };

    self.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf.progressView.progress = progress.fractionCompleted;
        });
    };

    AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
    [transferUtility enumerateToAssignBlocksForUploadTask:nil downloadTask:^(AWSS3TransferUtilityDownloadTask * _Nonnull downloadTask, AWSS3TransferUtilityProgressBlock  _Nullable __autoreleasing * _Nullable downloadProgressBlockReference, AWSS3TransferUtilityDownloadCompletionHandlerBlock  _Nullable __autoreleasing * _Nullable completionHandlerReference) {
        NSLog(@"%lu", (unsigned long)downloadTask.taskIdentifier);

        *downloadProgressBlockReference = weakSelf.progressBlock;
        *completionHandlerReference = weakSelf.completionHandler;

        dispatch_async(dispatch_get_main_queue(), ^{
            self.statusLabel.text = @"Uploading...";
        });
    }];
}

- (IBAction)start:(id)sender {
    self.imageView.image = nil;

    AWSS3TransferUtilityDownloadExpression *expression = [AWSS3TransferUtilityDownloadExpression new];
    expression.progressBlock = self.progressBlock;

    AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
    [[transferUtility downloadDataFromBucket:S3BucketName
                                         key:S3DownloadKeyName
                                  expression:expression
                            completionHander:self.completionHandler] continueWithBlock:^id(AWSTask *task) {
        if (task.error) {
            NSLog(@"Error: %@", task.error);
        }
        if (task.exception) {
            NSLog(@"Exception: %@", task.exception);
        }
        if (task.result) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.statusLabel.text = @"Downloading...";
            });
        }

        return nil;
    }];
}

Info.plist 文件修改于:

【问题讨论】:

    标签: ios amazon-web-services authentication amazon-s3 amazon-cognito


    【解决方案1】:

    您遇到的错误是在解释问题 - 您以未经身份验证的用户身份向 Cognito 发送请求,这意味着没有登录信息包含/链接到该身份,而池未启用该身份。

    未经身份验证的身份是身份池的一项选择加入功能,您必须在从 Cognito 控制台创建/编辑池时启用它。

    【讨论】:

    • 谢谢杰夫。我会要求数据库管理员启用此功能。或者,您如何验证 Cognito 用户?我们在联邦身份中创建了一个测试用户。很抱歉这个蹩脚的问题,只是想迈出第一步。
    • 哦,不用担心!经过身份验证的用户仅指已登录到也为池配置的某个身份提供者(即 Facebook、Google、Twitter 或链接的用户池)的用户。您可能设置的内容取决于您的应用程序的需求,但有一些示例可以向您展示如何使用各种提供程序:github.com/awslabs/aws-sdk-ios-samples/tree/master/…
    • 好的。感谢您的链接。我想我可能会要求禁用该功能。因此,基本上支持第三方提供商的重点是提高安全性。我正在开发的应用程序仅供内部使用,所以我可能不需要第一个版本。非常感谢您的解释。
    • 当然,在这种情况下,未经身份验证是一种可行的方法。你是对的,这是一个安全问题。一个常见的情况是为未经身份验证的用户提供低权限,然后增加他们在登录时可以执行的操作。将其视为访客访问。但是,对于您正在做的事情,这并没有多大意义,所以只进行 unauth 是有道理的。
    • 我的 aws 管理员说他们无法禁用未经身份验证的身份。但是,当我查看 AWS 文档时,我有点迷失了,找不到与我相关的示例(我发现的大多数示例指的是身份池 ID,而不是格式为 username +密码)。你能指出我正确的方向吗? AWS 管理员创建了一个用户名供我使用,但我真的不知道将这些凭证放在哪里。 docs.aws.amazon.com/cognito/latest/developerguide/…
    猜你喜欢
    • 2017-07-01
    • 2019-03-24
    • 2019-02-09
    • 1970-01-01
    • 2017-06-14
    • 2016-06-24
    • 2016-10-09
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多