抛出状态为 TrustFailure 的 WebException。 AmazonS3 .net 3.5 版本 3 从 Sharepoint 2010 调用

A WebException with status TrustFailure was thrown. AmazonS3 .net 3.5 Version 3 invoking from sharepoint 2010

目前我正在 POC 中使用 AmazonS3 Sdk for .net 3.5 版本 3 进行 CRUD 操作。我正在尝试使用密钥和访问密钥和存储桶名称检索特定存储桶名称的区域端点(位置)(有位置:欧盟(法兰克福)(eu-central-1))。为了建立连接 使用 AmazonS3 并执行 CRUD 操作 因此,当我尝试从共享点(我使用 SharePoint 的母版页创建自己的页面的网页)获取区域端点以使用区域检索创建 AmazonS3Client 实例时,我得到了状态为 TrustFailure 的 WebException。
使用以下代码:

private string defaultAmazonHttpsHost = "https://s3.amazonaws.com";
private string defaultAmazonHttpHost = "http://s3.amazonaws.com"; 

private Amazon.RegionEndpoint GetRegionEndpoint(string bucket, BasicAWSCredentials amazonCredentials, bool useSSL)
{
    Amazon.RegionEndpoint regiongEndpoint = null;
    AmazonS3Config configurationClient = new AmazonS3Config();
    configurationClient.UseHttp = !useSSL;
    configurationClient.ServiceURL = useSSL ? defaultAmazonHttpsHost : defaultAmazonHttpHost;
    try
    {
        using (AmazonS3Client clientConnection = new AmazonS3Client(amazonCredentials, configurationClient))
        {
            GetBucketLocationRequest locationRequest = new GetBucketLocationRequest();
            locationRequest.BucketName = bucket;
            string locationName = clientConnection.GetBucketLocation(locationRequest).Location.Value;
            if (locationName.Equals("EU", StringComparison.InvariantCultureIgnoreCase))
            {
                regiongEndpoint = Amazon.RegionEndpoint.EUWest1;
            }
            else if (string.IsNullOrEmpty(locationName))
            {
                regiongEndpoint = Amazon.RegionEndpoint.USEast1;
            }
            else
            {
                regiongEndpoint = Amazon.RegionEndpoint.GetBySystemName(locationName);
            }
        }
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
          throw amazonS3Exception;
    }
    catch (Exception unExpectedException)
    {
        throw unExpectedException;
    }
    return regiongEndpoint;
}
BasicAWSCredentials credentials = new BasicAWSCredentials("my access Key", "my secret key");
AmazonS3Config configurationAmazon = new AmazonS3Config();
configurationAmazon.RegionEndpoint = GetRegionEndpoint("bucketName", credentials, false);
 AmazonS3Client _s3 = new AmazonS3Client(credentials, configurationAmazon );

我的任务执行 CRUD 操作 + 测试与 AmazonS3 Sdk .net 3.5 版本 3 的连接,以及源信息: -密钥 - 访问密钥 - 存储桶名称

奇怪的是,如果这部分代码 运行(执行)来自另一个项目(没有共享点交互,例如:控制台项目)我没有得到这个异常)你知道问题是什么吗?

我在执行对 amazonS3 的任何请求之前使用了以下内容,现在它按预期工作我认为问题出在 sharepoint 使用的证书上。

ServicePointManager.ServerCertificateValidationCallback +=   
delegate(  
    object sender,   
    X509Certificate certificate,   
    X509Chain chain,   
    SslPolicyErrors sslPolicyErrors)  
    {  
         return true;  
    };  

post 提供了解释

这里的重点是"TrustFailure"。证书有问题。在我的例子中,这个错误是因为我的公司使用 Websense,一个网络 filter/security 套件,拦截并重新颁发网络流量的 https 证书,以便它可以监视你。即使您不使用类似的东西,底线是您的计算机不信任远程计算机所使用的证书的颁发者。我收到此错误的服务器在其受信任的根证书颁发机构中没有正确的证书。导入正确的可信根证书后 (Add trusted root certificate authority to local computer),我不再收到错误。

如果您不认为是这种情况,您可以通过将详细信息写入控制台或在 Console.WriteLine 此处放置断点并实际检查证书来获得有关异常情况的更多详细信息和 SSL 错误:

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

.....
.....
//before you make the request
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
delegate (
    object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
{
    Console.WriteLine("Subject: " + certificate.Subject + ", Issuer: " + certificate.Issuer + ". SSL Errors: " + sslPolicyErrors.ToString());
    return false;
};

这里的关键点是您需要找到证书问题并解决它,而不是通过忽略所有 ssl 错误让自己容易受到攻击。

TrustFailure 也可能是机器上的日期不正确造成的。