因远程方关闭传输流而导致身份验证失败异常,当从webservice获取响应时。

57

我正在调用第三方服务,当我请求响应时,它抛出了一个异常,说

"Authentication failed because the remote party has closed the transport stream exception".

我认为在发送凭据时存在问题。我甚至尝试提供新的凭据。以下是完整代码:

string get_url = "https://**.*******.com/com/******/webservices/public_webservice.cfc?wsdl&Method=CreateUser&SiteID=**&WSPassword=******&UserName=******";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(get_url);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
//request.UseDefaultCredentials = false;
//request.Credentials = new System.Net.NetworkCredential("*****", "*****");
request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";

// Show the sent stream
//lbl_send_stream.Text = send_stream;
//lbl_send_stream.Text = get_url;
// Get UserId And LoginToken From Third Party DB
// ==============================================
//Exception gets throwed When code hits here
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

在此输入图片描述


1
https://dev59.com/5mIk5IYBdhLWcg3wDKNb - Amit Kumar Ghosh
我尝试过了,它并没有解决问题。 - Chirag K
3个回答

144
我找到了答案,原因是我们调用的第三方网络服务不支持TLS 1.0,但支持1.1和1.2。因此我必须更改安全协议。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

5
在获取HTTP响应之前,你可以添加该内容到请求中。 - Chirag K
1
.Net 4.0不支持Tls11和Tls12。要访问它们,需要使用.Net 4.5或更高版本。 - Jinlye
13
如果使用 .Net 4.0 版本,你可以使用数值来表示 TLS1.2,如下所示: ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; - Young Bob
2
另请参阅https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/,了解其他选项和框架。 - Young Bob
我使用了这个解决方案,但在发出请求之前使用ServicePointManager.SecurityProtocol时无效,而在响应之前使用则有效。 - keivan kashani
显示剩余8条评论

1
我有同样的问题,最初我更改了安全协议,但没有起作用,然后我意识到我需要在创建 WebRequest 之前更改安全协议。
  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
        WebRequest request = WebRequest.Create(fullURL);
        request.Headers = requestHeaders;
        byte[] Bytes = System.Text.Encoding.ASCII.GetBytes(jsonData);

0

我有一个限制,只能使用TLS 1.2,并且最重要的是资源地址(URL / 地址)是本地的。因此,上面的解决方案对我没有用。在仔细分析web.config之后,我尝试使用<bypasslist>来处理本地URL,奇迹发生了!

<system.net>
    <defaultProxy>
      <proxy usesystemdefault="false" proxyaddress="<yourproxyaddress>" bypassonlocal="true" />
      <bypasslist>  
        <add address="[a-z]+\.abcd\.net\.au$" />  
      </bypasslist>
    </defaultProxy>
</system.net>

请注意,我已经在使用<proxy>设置来访问其他外部URL,因此不允许没有这个设置。希望这可以帮到你!

如果我在尝试使用CMD生成代码时遇到此错误,您有什么建议? - Zeeshan Adil
尝试这个: add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 <你的命令> - jitin14

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接