Reputation: 2077
I am consuming a web api which has client certificate authentication. I have both cert.pem, key.perm files. and I tested the api's in postman successfully by importing both files in certificate tab.. it works fine. but when i try to implement that api in my asp.net web application, it shows authentication failed error. i don't know how to use both cert.pem, key.perm files in authentication part of my coding.
I tried some codings.
string url = "https://uat-api.ssg-wsg.sg/courses/runs/50331/sessions?uen=S89PB0005D&courseReferenceNumber=PA-S89PB0005D-01-Fuchun 354&sessionMonth=012021";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
X509Certificate clientCertificate = X509Certificate.CreateFromCertFile(System.Web.HttpContext.Current.Server.MapPath("~/Certificates/cert.pem"));
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
WebReq.ClientCertificates.Add(clientCertificate);
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
Can anyone help me how to use both cert.pem, key.perm files in authentication part and make the api runs successfully.. Thank You.
Upvotes: 1
Views: 5570
Reputation: 892
I'm assuming that your cert.pem file is the certificate and the key.pem file contains the private key. If you are using .net 5, you can do something like this:
var certificatePem = File.ReadAllText("cert.pem"); //you have to provide the correct path here
var key = File.RealAllText("key.pem"); //and here
var certificate = X509Certificate2.CreateFromPem(certificatePem, key);
Note the use of the new X509Certificate2 class.
if my initial asumption is not true, please post the text within the pem files (you can strip off a portion of the text, or you can gray out the relevant parts, of course)
Upvotes: 4