2016-01-31 167 views
3

我使用C#从谷歌请求访问令牌:谷歌Analytics(分析)访问令牌 - TLS 1.1+

string serviceAccountEmail = ConfigurationManager.AppSettings["analyticsServiceAccountEmail"].ToString(); 
     string securityKey = ConfigurationManager.AppSettings["analyticsSecurityKeyLocation"].ToString(); 
     string password = ConfigurationManager.AppSettings["analyticsSecurityPassword"].ToString(); 

     var certificate = new X509Certificate2(securityKey, password, X509KeyStorageFlags.Exportable); 

     var scopes = new List<string> { "https://www.googleapis.com/auth/analytics.readonly", "https://www.googleapis.com/auth/analytics" }; 

     ServiceAccountCredential credential = new ServiceAccountCredential(
     new ServiceAccountCredential.Initializer(serviceAccountEmail) 
     { 
      Scopes = scopes 
     }.FromCertificate(certificate)); 

     Task<bool> task = credential.RequestAccessTokenAsync(CancellationToken.None); 

     task.Wait(); 

     if (!task.Result || credential.Token == null || string.IsNullOrEmpty(credential.Token.AccessToken)) 
     { 
     throw new Exception("Failed to get token from Google"); 
     } 

     return credential.Token.AccessToken; 

我不得不禁用TLS 1.0 PCI合规性。既然我已经做了,该代码与以下错误打破:

One or more errors occurred.: An error occurred while sending the request.: The underlying connection was closed: An unexpected error occurred on a receive.: The client and server cannot communicate, because they do not possess a common algorithm

任何建议,我怎么可以让使用TLS 1.1或更高版本的电话吗?

+0

查看此[链接](https://github.com/LindaLawton/Google-Dotnet-Samples/tree/master/Google-Analytics)。它有很好的信息。 –

回答

0

它已经在的Application_Start通过Global.asax中来完成:

请仔细阅读本你做出改变之前:How do I disable SSL fallback and use only TLS for outbound connections in .NET? (Poodle mitigation)

做到这一点的方法是:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12 

这将打开关于SSL3的通信支持,如果适用,则回退到TLS 1.1或TLS 1.2。

+0

不是SSL 3.0比TLS 1.1更安全吗?无论如何,我只需要TLS 1.1+。当我这样做时,Google Analytics的呼叫失败。它只适用于我启用TLS 1.0的情况。 – user472292