使用 AWS SDK for .NET 中缺少的 SessionToken 上传到 S3 存储桶
Uploading to S3 bucket using missing SessionToken in AWS SDK for .NET
我正在关注 this tutorial 将文件上传到 AWS S3 存储桶。
作者正在使用 AppConfiguration
配置 AWS:
public class AppConfiguration : IAppConfiguration
{
// Keep the following details in appsettings.config file or DB or Enivironment variable
// Get those values from it and assign to the below varibales. Based on the approach , modify the below code.
public AppConfiguration()
{
BucketName = "";
Region = "";
AwsAccessKey = "";
AwsSecretAccessKey = "";
AwsSessionToken = "";
}
public string BucketName { get; set; }
public string Region { get; set; }
public string AwsAccessKey { get; set; }
public string AwsSecretAccessKey { get; set; }
public string AwsSessionToken { get; set; }
}
除此之外,还有一个 AwsSessionToken
属性,我不知道如何 retrieve/generate。
如何检索会话令牌?
TLDR:使用 shared AWS credentials file 并删除应用程序中与凭据相关的所有代码,包括凭据本身,因为它们存在安全风险。
永远,永远,永远,永远,永远,永远,永远,永远,永远在应用程序文件中存储凭据。
以下是 AWS SDK for .NET Developer Guide 发出重要警告的凭据:
Do NOT put literal access keys in your application files. If you do, you create a risk of accidentally exposing your credentials if, for example, you upload the project to a public repository.
Do NOT include files that contain credentials in your project area.
这几乎到处都是重申else:
We recommend that you never add your AWS access keys directly to the client in any production files. Many developers have had their account compromised by leaked keys.
而是使用在 default credentials provider chain for the .NET SDK 中提供凭据的方法来安全地向您的 .NET 应用程序提供凭据。
它比创建 POCO 或在您的代码中处理凭据更容易、更清晰。
对于本地开发,我总是更喜欢 shared AWS credentials file,您可以自己创建它或使用 aws configure
通过 AWS CLI 进行设置。然后,AWS SDK 将自动获取凭据,您无需在代码中指定任何内容。
使用 AWS 凭证文件提供以下 benefits:
- 您的项目凭据存储在您的项目之外,因此
不可能不小心将它们提交到版本中
控制。
- 您可以在一个地方定义和命名多组凭据。
- 您可以轻松地在项目之间重复使用相同的凭据。
- 其他 AWS SDK 和工具支持,同一个凭证文件。这个
允许您在其他工具中重复使用您的凭据。
无论您做什么,请不要对凭据进行硬编码。
仅供参考:会话令牌用于验证临时 AWS 凭证。您的访问密钥 ID 和秘密访问密钥是永久身份验证方法,直到它们成为 deactivated/deleted,因此您根本不需要担心会话令牌。
我正在关注 this tutorial 将文件上传到 AWS S3 存储桶。
作者正在使用 AppConfiguration
配置 AWS:
public class AppConfiguration : IAppConfiguration
{
// Keep the following details in appsettings.config file or DB or Enivironment variable
// Get those values from it and assign to the below varibales. Based on the approach , modify the below code.
public AppConfiguration()
{
BucketName = "";
Region = "";
AwsAccessKey = "";
AwsSecretAccessKey = "";
AwsSessionToken = "";
}
public string BucketName { get; set; }
public string Region { get; set; }
public string AwsAccessKey { get; set; }
public string AwsSecretAccessKey { get; set; }
public string AwsSessionToken { get; set; }
}
除此之外,还有一个 AwsSessionToken
属性,我不知道如何 retrieve/generate。
如何检索会话令牌?
TLDR:使用 shared AWS credentials file 并删除应用程序中与凭据相关的所有代码,包括凭据本身,因为它们存在安全风险。
永远,永远,永远,永远,永远,永远,永远,永远,永远在应用程序文件中存储凭据。
以下是 AWS SDK for .NET Developer Guide 发出重要警告的凭据:
Do NOT put literal access keys in your application files. If you do, you create a risk of accidentally exposing your credentials if, for example, you upload the project to a public repository.
Do NOT include files that contain credentials in your project area.
这几乎到处都是重申else:
We recommend that you never add your AWS access keys directly to the client in any production files. Many developers have had their account compromised by leaked keys.
而是使用在 default credentials provider chain for the .NET SDK 中提供凭据的方法来安全地向您的 .NET 应用程序提供凭据。
它比创建 POCO 或在您的代码中处理凭据更容易、更清晰。
对于本地开发,我总是更喜欢 shared AWS credentials file,您可以自己创建它或使用 aws configure
通过 AWS CLI 进行设置。然后,AWS SDK 将自动获取凭据,您无需在代码中指定任何内容。
使用 AWS 凭证文件提供以下 benefits:
- 您的项目凭据存储在您的项目之外,因此 不可能不小心将它们提交到版本中 控制。
- 您可以在一个地方定义和命名多组凭据。
- 您可以轻松地在项目之间重复使用相同的凭据。
- 其他 AWS SDK 和工具支持,同一个凭证文件。这个 允许您在其他工具中重复使用您的凭据。
无论您做什么,请不要对凭据进行硬编码。
仅供参考:会话令牌用于验证临时 AWS 凭证。您的访问密钥 ID 和秘密访问密钥是永久身份验证方法,直到它们成为 deactivated/deleted,因此您根本不需要担心会话令牌。