c# - Mono https webrequest 失败,返回 "The authentication or decryption has failed"

标签 c# https mono webrequest

我正在制作一个简单的 REST 客户端以在我的 C# 应用程序中使用。在 Windows 上的 .net 中,它非常适合 http://和 https://连接。在 Ubuntu 10.10 上的单声道 2.6.7(也用 2.8 测试过,结果相同)只有 http://有效。 https://连接在 request.GetResponse() 方法上抛出此异常:

Unhandled Exception: System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 

我还没有找到解决这个问题的方法。任何人都知道为什么会发生这种情况以及如何解决它?

同样,这仅在 Mono 中失败,.Net 似乎在建立连接时没有任何问题。

调用代码如下:

public JToken DoRequest(string path, params string[] parameters) {
    if(!path.StartsWith("/")) {
        path = "/" + path;
    }
    string fullUrl = url + path + ToQueryString(parameters);

    if(DebugUrls) Console.WriteLine("Requesting: {0}", fullUrl);

    WebRequest request = HttpWebRequest.CreateDefault(new Uri(fullUrl));
    using(WebResponse response = request.GetResponse())
    using(Stream responseStream = response.GetResponseStream()) {
        return ReadResponse(responseStream);
    }
}

最佳答案

我在使用 Unity(它也使用单声道)和 this post 时遇到了同样的问题帮我解决了。

只需在提出请求之前添加以下行:

ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

这个方法:

public bool MyRemoteCertificateValidationCallback(System.Object sender,
    X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    bool isOk = true;
    // If there are errors in the certificate chain,
    // look at each error to determine the cause.
    if (sslPolicyErrors != SslPolicyErrors.None) {
        for (int i=0; i<chain.ChainStatus.Length; i++) {
            if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown) {
                continue;
            }
            chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
            chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
            chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
            chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
            bool chainIsValid = chain.Build ((X509Certificate2)certificate);
            if (!chainIsValid) {
                isOk = false;
                break;
            }
        }
    }
    return isOk;
}

关于c# - Mono https webrequest 失败,返回 "The authentication or decryption has failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4926676/

相关文章:

google-app-engine - 是否可以在 App Engine 站点上使用 https ://yourdomain. com?

ssl - StartSSL SSL 证书在浏览器中显示为 net::ERR_CERT_AUTHORITY_INVALID

linux - F# 代码在 REPL 中工作正常,但在编译期间出现堆栈溢出

c# - 将 Azure KeyVault key 转换为 RSACng 或 RSACryptoServiceProvider

c# - 这应该被 mock 还是被打断?

c# - 何时使用属性与方法?

c# - 将 Assert.That(而不是 Assume.That)与 [Theory] ​​一起使用是错误的吗?

asp.net - 即使数据不敏感,我是否需要在用户登录后保持 HTTPS (SSL) 状态?

c# - 在 iPhone 上运行时,RestSharp 与 Unity3d 一起崩溃

iphone - 在 monotouch 中处理 json 的最佳方法是什么