c# - Mono https webrequest fails with "The authentication or decryption has failed" -
i'm making simple rest client use in c# applications. in .net on windows works great http:// , https:// connections. in mono 2.6.7 (also tested 2.8 same results) on ubuntu 10.10 http:// works. https:// connections throw exception on request.getresponse() method:
unhandled exception: system.net.webexception: error getting response stream (write: authentication or decryption has failed.): sendfailure ---> system.io.ioexception: authentication or decryption has failed. ---> mono.security.protocol.tls.tlsexception: invalid certificate received server. error code: 0xffffffff800b010a @ mono.security.protocol.tls.handshake.client.tlsservercertificate.validatecertificates (mono.security.x509.x509certificatecollection certificates) [0x00000] in <filename unknown>:0 @ mono.security.protocol.tls.handshake.client.tlsservercertificate.processastls1 () [0x00000] in <filename unknown>:0 @ mono.security.protocol.tls.handshake.handshakemessage.process () [0x00000] in <filename unknown>:0 @ (wrapper remoting-invoke-with-check) mono.security.protocol.tls.handshake.handshakemessage:process () @ mono.security.protocol.tls.clientrecordprotocol.processhandshakemessage (mono.security.protocol.tls.tlsstream handmsg) [0x00000] in <filename unknown>:0 @ mono.security.protocol.tls.recordprotocol.internalreceiverecordcallback (iasyncresult asyncresult) [0x00000] in <filename unknown>:0 --- end of inner exception stack trace --- @ mono.security.protocol.tls.sslstreambase.asynchandshakecallback (iasyncresult asyncresult) [0x00000] in <filename unknown>:0 --- end of inner exception stack trace --- @ system.net.httpwebrequest.endgetresponse (iasyncresult asyncresult) [0x00000] in <filename unknown>:0 @ system.net.httpwebrequest.getresponse () [0x00000] in <filename unknown>:0
i haven't been able find way fix this. have idea why happening , how fix it?
again, fails in mono, .net doesn't seem have problem establishing connection.
here's calling code:
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); } }
i had same problem unity (which uses mono) , this post helped me solve it.
just add following line before making request:
servicepointmanager.servercertificatevalidationcallback = myremotecertificatevalidationcallback;
and method:
public bool myremotecertificatevalidationcallback(system.object sender, x509certificate certificate, x509chain chain, sslpolicyerrors sslpolicyerrors) { bool isok = true; // if there errors in certificate chain, // @ each error determine cause. if (sslpolicyerrors != sslpolicyerrors.none) { (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; }
Comments
Post a Comment