c# - 使用 C# SDK 获取 AWS 调用者身份
问题描述
当我使用 aws cli 执行此操作时,i.ex. 在 fargate 任务中,我可以看到UserId
我的应用程序将要使用的
aws sts get-caller-identity
在控制台上有这个输出
{
"Arn": "arn:aws:sts::643518765421:assumed-role/url_process_role/6ae81f92-66f3-30de-1eaa-3a7d1902bad9",
"UserId": "ARDYOAZLVOAQXTT5ZXTV4:4ea81f97-66f3-40de-beaa-3a7d1902bad9",
"Account": "692438514791"
}
我想获得相同的信息,但使用 C# SDK。我尝试使用此文档中公开的方法,但我可以看到一些与帐户相关的详细信息,但看不到UserId
分配的。
到目前为止,我已经尝试过,但在 Fargate 任务中运行时我看不到任何配置文件。
var awsChain = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain();
System.Console.WriteLine($"Found {awsChain.ListProfiles().Count} AWS profiles.");
我的最终目标是获取它并添加到使用 Fargate 处理的某些任务中,以便在出现故障时将相关 ID 保存在数据库中,并轻松找到 Fargate 日志流。
解决方案
IAmazonSecurityTokenService
使用 .netcore 执行时将提供相同的信息。请注意,上面的示例只能在 AWS 域内工作,因为如果从开发机器进行测试,端点是不公开的。
var getSessionTokenRequest = new GetSessionTokenRequest
{
DurationSeconds = 7200
};
var stsClient = hostContext.Configuration.GetAWSOptions().CreateServiceClient<IAmazonSecurityTokenService>();
var iden = stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest { }).Result;
System.Console.WriteLine($"A={iden.Account} ARN={iden.Arn} U={iden.UserId}");
推荐阅读
- reactjs - React,如何从子组件加载状态/属性
- python - 如何按每一行拆分熊猫数据框并包含每个创建的新数据框的标题?
- javascript - 将文本添加到文本输入 onKeyPress react.js
- java - 如何使用 DateTimeFormatter 用冒号解析偏移量?
- python - 如何在 NumPy 中一次乘以 3 个以上的向量
- node.js - 如何使用 vscode 中的共享打字稿项目解决“属性不存在错误”?
- java - 如何使用resttemplate发送和接收列表?
- r - devtools::check() 完成时没有错误、警告或消息,travis 构建失败 (devtools::dev_package_deps)
- azure-data-factory - Azure 数据工厂:用于分层架构映射的对象名称中的空格
- c - mlockall 和共享库