0

我目前正在将第三方制作的 ASP.NET 应用程序从 Windows 移动到 Linux。我阅读了文档,没有任何迹象表明这应该是一个问题,但遗憾的是

var profile = new CredentialProfile(profileName, credentials) {
    Region = RegionEndpoint.EUWest1
};

var netSDKFile = new NetSDKCredentialsFile();
netSDKFile.RegisterProfile(profile);

引发以下异常

Unhandled Exception: Amazon.Runtime.AmazonClientException: The encrypted store is not available.  This may be due to use of a non-Windows operating system or Windows Nano Server, or the current user account may not have its profile loaded.
   at Amazon.Util.Internal.SettingsManager.EnsureAvailable()
   at Amazon.Runtime.CredentialManagement.NetSDKCredentialsFile..ctor()

Linux 是否不支持 Amazon .NET SDK(或其中的一部分)?如果是这种情况,是否有可能的解决方法?

4

2 回答 2

2

在大多数情况下,Linux 上不支持的内容很少,而 Windows 上则支持。NetSDKCredentialsFile除了它使用 Win32 API 来加密凭据这一事实之外,我想不出任何东西。

您可以使用SharedCredentialsFile在存储在下的凭据文件中注册配置文件~/.aws/credentials。这是所有其他 AWS 开发工具包和工具支持的存储凭证。

于 2019-08-13T23:37:54.330 回答
0

根据 Norm 的回答,我找到了解释如何使用共享凭证的资源:https ://medium.com/@somchat/programming-using-aws-net-sdk-9ce3f5119633

这就是我之前使用 NetSDKCredentials 的方式,它不适用于 Linux/Mac OS:

//Try this code on a non-Windows platform and you will see the above error
var options = new CredentialProfileOptions
  {
    AccessKey = "access_key",
    SecretKey = "secret_key"
  };
var profile = new CredentialProfile("default", options);
profile.Region = RegionEndpoint.USWest1;
NetSDKCredentialsFile file = new NetSDKCredentialsFile();
file.RegisterProfile(profile);

但是我随后能够使用此示例来使用 SharedCredentials:

var credProfileStoreChain = new CredentialProfileStoreChain();
if (credProfileStoreChain.TryGetAWSCredentials("default", out AWSCredentials awsCredentials))
{
  Console.WriteLine("Access Key: " + awsCredentials.GetCredentials().AccessKey);
  Console.WriteLine("Secret Key: " + awsCredentials.GetCredentials().SecretKey);
}
Console.WriteLine("Hello World!");

然后,您将能够看到您的代码能够访问密钥:

Access Key: A..................Q
Secret Key: 8.......................................p
Hello World!

然后我使用 System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform() (因为我在 Windows 和 Linux 上都使用此代码)来确定要使用的凭据:

using System.Runtime.InteropServices;

//NETSDK Credentials only work on Windows - must use SharedCredentials on Linux
bool isLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

if (isLinux) {

    //Use SharedCredentials
            
} else { 

    //Use NetSDKCredentials

}

您可能会发现 AWS 文档的这一部分也很有帮助:https ://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#creds-locate

于 2021-03-10T10:13:00.267 回答