我可以使用我的 Cognito 凭证通过适用于 .NET 的 AWS 开发工具包对 AWS 服务进行身份验证吗?

问题描述 投票:0回答:1

我想使用 Cognito 用户将文件从 S3 存储桶下载到 Windows 计算机。

我创建了一个用户池,创建了一个新的演示用户,还创建了一个启用了授权代码流的应用程序客户端。之后,我创建了一个新角色,该角色仅提供对 AWS S3 服务的完全访问权限,将该角色分配给一个组,并将我的演示用户分配给该组。

在我的 .NET 8 应用程序中,我打开 Web 浏览器,将用户重定向到 Cognito 登录页面并重定向回应用程序的 Web 服务器。通过收集到的授权代码,我获得了 OAuth 2.0 令牌。

我现在需要使用安全令牌服务承担一个角色,提供我的 OAuth 2.0 凭证,然后接收 AWS 凭证,然后可以使用该凭证通过 SDK 调用 S3。

我正在使用

AmazonSecurityTokenServiceClient
,但我收到错误:

无法从 EC2 实例元数据服务获取 IAM 安全凭证

EC2服务如何发挥作用?

AmazonSecurityTokenServiceClient
有什么问题吗?

而且,如何使用我的 Cognito 凭据访问 S3?

这是我的代码:

// Get AWS credentials using the identity pool
AmazonSecurityTokenServiceClient securityTokenServiceClient = new AmazonSecurityTokenServiceClient(RegionEndpoint.EUNorth1);
AssumeRoleWithWebIdentityResponse roleResponse = await securityTokenServiceClient.AssumeRoleWithWebIdentityAsync(new AssumeRoleWithWebIdentityRequest()
{
    WebIdentityToken = tokenResponse?.IdToken,
    RoleArn = "arn:aws:iam::1234:role/cognito-demo-bucket",
    RoleSessionName = "demo"
});

_logger.LogTrace("Access key: {awsCredentials}\nSecret access key: {secretAccessKey}", roleResponse.Credentials.AccessKeyId, roleResponse.Credentials.SecretAccessKey);
c# amazon-web-services amazon-s3 amazon-cognito aws-sdk-net
1个回答
0
投票

EC2服务如何发挥作用?

您获得

Unable to get IAM security credentials from EC2 Instance Metadata Service
的原因在于适用于 .NET 的 AWS 开发工具包如何搜索凭证。

.NET SDK 的

默认凭据提供程序链按照特定顺序一一搜索凭据。如果一切都失败了,目前,凭证提供者最终要考虑的地方是:

    Amazon EC2 实例元数据。
由于 .NET SDK 在其他 7 个位置的任何位置都找不到您的应用程序的任何凭证,因此其最终尝试是从元数据项下的 EC2 实例元数据端点加载它们

iam/security-credentials/{role-name}


由于您没有在 EC2 实例上运行,因此也会失败,并且 SDK 会出错并显示上述错误消息 - 基本上是说:

我在任何地方都找不到任何凭据。


AmazonSecurityTokenServiceClient

有什么问题吗?


本例中的
AmazonSecurityTokenServiceClient

不需要

任何 AWS 凭证
。它用于调用 AssumeRoleWithWebIdentity
 操作,该操作将 Cognito ID 令牌交换为临时 AWS 凭证。
根据文档:

调用
AssumeRoleWithWebIdentity

不需要使用 AWS 安全凭证

因此,您需要告诉AWS SDK不要签署请求。

对于 .NET SDK,您可以使用

AnonymousAWSCredentials

 类来完成此操作:
using Amazon; using Amazon.Runtime; using Amazon.SecurityToken; var anonymousCredentials = new AnonymousAWSCredentials(); var stsClient = new AmazonSecurityTokenServiceClient(anonymousCredentials, RegionEndpoint.EUWest1);



而且,如何使用我的 Cognito 凭据访问 S3?

使用

SessionAWSCredentials

;对于 Amazon S3,这是一个完整的、最小的示例,其中列出了存储桶中的对象:
using Amazon; using Amazon.Runtime; using Amazon.S3; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; var anonymousCredentials = new AnonymousAWSCredentials(); var stsClient = new AmazonSecurityTokenServiceClient(anonymousCredentials, RegionEndpoint.EUWest1); var assumeRoleResponse = await stsClient.AssumeRoleWithWebIdentityAsync(new AssumeRoleWithWebIdentityRequest { RoleArn = "arn:aws:iam::1234:role/cognito-demo-bucket", RoleSessionName = "xxx", WebIdentityToken = "yyy" }); var assumedCredentials = new SessionAWSCredentials( assumeRoleResponse.Credentials.AccessKeyId, assumeRoleResponse.Credentials.SecretAccessKey, assumeRoleResponse.Credentials.SessionToken ); var s3Client = new AmazonS3Client(assumedCredentials); var bucketName = "xyz"; var listObjectsResponse = await s3Client.ListObjectsAsync(bucketName);

© www.soinside.com 2019 - 2024. All rights reserved.