【问题标题】:.NET Core 3.x setting development AWS credentials.NET Core 3.x 设置开发 AWS 凭证
【发布时间】:2020-11-07 06:18:24
【问题描述】:

我的 EC2 实例(通过 Elastic Beanstalk)运行我的 ASP.Net Core 3.1 Web 应用程序没有问题。 AWS 凭证包含在与实例一起配置的密钥对中。

我现在想将我的数据保护密钥存储在我为它们创建的 S3 存储桶中,这样我就可以在所有 EC2 实例之间共享这些密钥。但是,当我在 Startup.ConfigureServices 中添加此服务时,我在本地收到运行时错误:

services.AddDefaultAWSOptions(Configuration.GetAWSOptions("AWS"));
services.AddAWSService<IAmazonS3>();
services.AddDataProtection()
    .SetApplicationName("Crums")
    .PersistKeysToAWSSystemsManager("/CrumsWeb/DataProtection");

如果我注释掉上面的 .PersistKeysToAWSSystemsManager("/CrumsWeb/DataProtection"); 行,我的应用程序在本地运行良好。当我取消注释该行时,会发生错误。所以这与它有关,但我似乎无法弄清楚。

我打算使用 hotchkj 的 PersistKeysToAwsS3,但当 AWS 推出 PersistKeysToAWSSystemsManager 时,它已被弃用。

运行时错误AmazonClientException:未配置 RegionEndpoint 或 ServiceURL 在我的 Program.cs 中的 CreateHostBuilder 上发生:

我已经花了很多时间尝试让 Visual Studio 2019 在本地运行我的应用程序,使用这些网站的建议:

https://aws.amazon.com/blogs/developer/configuring-aws-sdk-with-net-core/

https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html

ASP NET Core AWS No RegionEndpoint or ServiceURL configured when deployed to Heroku

No RegionEndpoint or ServiceURL configured

https://github.com/secretorange/aws-aspnetcore-environment-startup

https://www.youtube.com/watch?v=C4AyfV3Z3xs&ab_channel=AmazonWebServices

我的 appsettings.Development.json(我也在 appsettings.json 中尝试过)包含:

"AWS": {
  "Profile": "default",
  "Region": "us-east-1",
  "ProfilesLocation": "C:\\Users\\username\\.aws\\credentials"
}

并且凭证文件包含:

[default]
aws_access_key_id = MY_ACCESS_KEY
aws_secret_access_key = MY_SECRET_KEY
region = us-east-1
toolkit_artifact_guid=GUID

【问题讨论】:

    标签: amazon-s3 aws-sdk-net


    【解决方案1】:

    我最终放弃了 PersistKeysToAWSSystemsManager 来存储我的数据保护密钥,因为我不想再设置另一个 AWS 服务来将密钥存储在他们的 Systems Manager 中。我已经为 S3 帐户付费,所以我选择使用已弃用的 NuGet 包AspNetCore.DataProtection.Aws.S3。

    我在为密钥创建的存储桶上使用服务器端加密。这是 Startup.cs 中的代码:

        services.AddDataProtection()
            .SetApplicationName("AppName")
            .PersistKeysToAwsS3(new AmazonS3Client(RegionEndpoint.USEast1), new S3XmlRepositoryConfig("S3BucketName")
            {
                KeyPrefix = "DataProtectionKeys/",  // Folder in the S3 bucket for keys
            });
    

    注意 PersistKeysToAwsS3 中的 RegionEndpoint 参数,它解决了 No RegionEndpoint or ServiceURL Configured 错误。

    我将 AmazonS3FullAccess 策略添加到在我的所有实例中运行的 IAM 角色。 这为实例提供了访问 S3 存储桶的权限。我的本地开发计算机似乎也能够访问 S3 存储桶,尽管我不知道它从哪里获取凭据。我尝试了多次迭代 appsettings.json 和凭据文件更改以本地设置区域和凭据,但没有任何效果。也许它使用了我在设置 AWS Toolkit in Visual Studio 时输入的凭据。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 2019-04-24
      • 1970-01-01
      • 2018-04-26
      相关资源
      最近更新 更多