无法通过 C# 连接到 AWS SDK AmazonEC2Client

Unable to connect to AWS SDK AmazonEC2Client through C#

我正在尝试通过 SDK 连接到 AWS,我发现的示例说通过以下方法连接:

string accessKey = "****************GCDE";    
string SecretAccessKey = "****************oeE7";    
AmazonEC2Client(accessKey,SecretAccessKey,RegionEndpoint.USWest2);

DescribeInstancesRequest request = new DescribeInstancesRequest();

但这会引发以下异常...

HResult=0x80131500 Message=AWS 无法验证提供的访问凭证 来源=AWSSDK.Core

经过几个小时的摸索和在线查看不同的样本,我终于弄明白了。由于我使用的是 Microsoft 单点登录 (SSO),因此需要验证令牌。

string accessKey = "****************GCDE";    
string SecretAccessKey = "****************oeE7";    
string sessionToken = "****************oeE7";    

var tempCredentials = new SessionAWSCredentials(
            accessKey,
            SecretAccessKey,
            sessionToken);

 var _client = new AmazonEC2Client(tempCredentials,RegionEndpoint.USWest2);
 DescribeInstancesRequest request = new DescribeInstancesRequest();