【问题标题】:Error when trying to connect to HTTPS尝试连接到 HTTPS 时出错
【发布时间】:2014-10-01 03:25:33
【问题描述】:

当我尝试连接到 HTTPS REST 服务时,我在执行期间收到以下错误(目前在 VS 2013 的单元测试中):

System.Net.Http.HttpRequestException:发送时出错 请求。 ---> System.Net.WebException:请求被中止: 无法创建 SSL/TLS 安全通道。结果堆栈跟踪:在 System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult、TransportContext& 上下文)

代码:

public class CompanyApi
    {
        private string _username;
        private string _password;
        private string _url;

        public CompanyApi(string username, string password, string url)
        {
            _username = username;
            _password = password;
            _url = url;
        }

        public string MakeUrl(string programHostName)
        {
            return string.Format("https://{0}/api/v1/program/", programHostName);
        }

        /// <summary>Creates a basic HTTP authentication token.</summary>
        protected AuthenticationHeaderValue CreateBasicCredentials()
        {
            string token = String.Format("{0}:{1}", _username, _password);
            return new AuthenticationHeaderValue("Basic",
                                                 Convert.ToBase64String(Encoding.UTF8.GetBytes(token))
                );
        }

        public void UserGivePoints(add_points newPointsRequest)
        {
            string url = MakeUrl(_url);
            WebRequestHandler handler =
                new WebRequestHandler
                {
                    AllowAutoRedirect = false,
                    UseProxy = false
                };

            HttpClient client =
                new HttpClient(handler)
                {
                    BaseAddress = new Uri(url)
                };
            client.DefaultRequestHeaders.Authorization = CreateBasicCredentials();

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/x-javascript"));

            // Create the JSON formatter.
            MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();

            // Use the JSON formatter to create the content of the request body.
            HttpContent content = new ObjectContent<add_points>(newPointsRequest, jsonFormatter);

            client.Timeout = new TimeSpan(0, 0, 30);

            HttpResponseMessage message = client.PostAsync("user/add_points?outputFormat=JSON", content).Result;
            if (message.StatusCode != HttpStatusCode.OK)
                throw new Exception("Wrong result code - " + message.StatusCode);
            message.EnsureSuccessStatusCode();
        }
    }

    public class add_points
    {
        public string username { get; set; }
        public int amount { get; set; }
        public string account_name { get; set; }
        public string reason { get; set; }

        public add_points(string userId, int pointAmount, string account, string pointDescription)
        {
            username = userId;
            amount = pointAmount;
            account_name = account;
            reason = pointDescription;
        }

        public add_points()
        { }
    }

据我从错误中可以看出,它似乎不喜欢网站本身的证书,这是我从 Chrome 收到的警告消息:

您试图访问贡品.site.com.int01.site.com,但 您实际上到达了一个标识为 *.int01.site.com 的服务器。 这可能是由于服务器上的错误配置或某些原因造成的 更认真。您网络上的攻击者可能正试图让您 访问虚假(且可能有害)版本的 贡品.site.com.int01.site.com。你不应该继续,特别是如果 您以前从未见过此网站的此警告。

我无法解决这个问题,供应商向我保证,许多其他人都可以毫无问题地连接到 API...

有什么想法吗?

2014-08-08 更新

我加了一个

ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }

WebRequestHandler 初始化代码中的一行,仍然遇到同样的问题。

【问题讨论】:

  • 当我尝试运行openssl s_client -connect tribute.site.com.int01.site.com:443 时,我得到gethostbyname failure。你确定这是正确的吗?如果不正确,请提供正确的信息,以便我们为您提供帮助。
  • 不,这是不正确的,因为我无法与公开的公众分享真实的网址

标签: api rest ssl https


【解决方案1】:

这里有几个选项:

解决方法:

  • 为您的 Https 请求禁用证书验证。您可以在 ServerCertificateValidationCallback 的帮助下做到这一点。
  • 将此证书添加为信任证书。

首选:

  • 请第三方使用有效证书。

【讨论】:

    【解决方案2】:

    主机名*.int01.site.com 的证书将仅匹配com.int01.site.com,但不匹配tribute.site.com.int01.site.com。通配符仅在同一级别上匹配。为了使该证书有效,他们需要一个能够匹配*.*.*.int01.site.com 的 SSL 证书。

    我强烈建议避免使用任何会禁用证书检查并与供应商合作以获得有效证书的代码。禁用或绕过证书检查只会降低安全性。如果您确实必须禁用检查,请在发送数据之前使用证书固定来验证供应商证书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2019-05-25
      • 2019-01-14
      相关资源
      最近更新 更多