WebRequest 创建https协议时 调用 GetRequestStream 报错

gavinluo 2017-06-15 05:11:32
环境是 Windows 2008 R2 , IIS7.5
问题:
WebRequest 创建https请求时 调用 GetRequestStream 报错
The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()

原因分析: IIS_IUser 权限不足 , GetRequestStream的时候需要访问证书?

解决办法: 设置应用程序池的标识为 localSystem. 但这个方法安全会有隐患.

不知道大家有好的方案没?
...全文
451 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 2017-06-16
  • 打赏
  • 举报
回复

           if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version11;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }

 private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; 
        }
其他都一样 没什么区别.. 请求过https的 没问题,.
编程有钱人了 2017-06-16
  • 打赏
  • 举报
回复
你访问的是HTTPS 网站,同样的问题也困扰我了,服务器注意开启443端口 下面试 paypal的是个例子

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                     string actionAddress = "https://www.paypal.com/cgi-bin/webscr";
            System.Net.HttpWebRequest req = (HttpWebRequest)WebRequest.Create(actionAddress);
            req.Method = "POST";
            req.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = Request.BinaryRead(Request.ContentLength);
            string strFormValues = Encoding.ASCII.GetString(param);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 和 req.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); 比较重要

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧