c# - How to make secure request to REST, having signed Certificate with Public key, and Private Key generated with Certificate Request -
i need make https request restapi. before, made certificate request , private key, using openssl. after, certificate request had been signed provider , took certificate. have certificate , private key. question: how make secure request using certificate, private key , .net features. provide code, wrote of intuition(note: used bouncycustle read pem files):
static void main(string[] args) { try { x509certificate certificate = getcertificate("c:/xxx/certificate.pem"); asymmetrickeyparameter privatekey = getprivatekey("c:/xxx/key.pem"); httpwebrequest webrequest = preparewebrequest("https://xxxx", "get", certificate, privatekey); webresponse webresponse = webrequest.getresponse(); } catch(exception e) { console.writeline("exception: {0}", e.message); console.write("stack trace: {0}", e.stacktrace); } console.readkey(); } private static x509certificate getcertificate(string pathtosertificate) { x509certificate certificatebouncycustle; using (var filestream = new filestream(pathtosertificate, filemode.open)) using (var streamreader = new streamreader(filestream)) { var pemreader = new pemreader(streamreader); certificatebouncycustle = (x509certificate)pemreader.readobject(); } return certificatebouncycustle; } private static asymmetrickeyparameter getprivatekey(string pathtoprivatekey) { asymmetrickeyparameter privatekey; using (var filestream = new filestream(pathtoprivatekey, filemode.open)) using (var streamreader = new streamreader(filestream)) { var pemreader = new pemreader(streamreader); privatekey = ((asymmetriccipherkeypair)pemreader.readobject()).private; } return privatekey; } private static httpwebrequest preparewebrequest(string url, string method, x509certificate certificate, asymmetrickeyparameter privatekey) { httpwebrequest httpwebrequest = (httpwebrequest)webrequest.create(url); httpwebrequest.method = method; httpwebrequest.clientcertificates.add(dotnetutilities.tox509certificate(certificate)); httpwebrequest.contenttype = "application/xml;charset=utf8"; return httpwebrequest; }
Comments
Post a Comment