Daniel
Daniel

Reputation: 136

How to send a RestSharp request with a certificate attached in a UWP application?

I created a .NET Core console app that performs exactly what I need: send a RestSharp request with an attached certificate. Everything works fine. Here's the code:

        static void Main(string[] args)
    {
        var client = new RestSharp.RestClient();
        client.ClientCertificates = new X509CertificateCollection();
        client.ClientCertificates.Add(new X509Certificate2(@"C:\Users\kid_j\Downloads\badssl.com-client.p12", "badssl.com"));
        var result = client.Execute(new RestSharp.RestRequest("https://client.badssl.com/", RestSharp.Method.GET));
        Console.WriteLine(result.StatusCode);
    }

But when I place that same code inside a UWP app, it does not work. I get this error message:

An error occurred while sending the request. Client certificate was not found in the personal (\"MY\") certificate store. In UWP, client certificates are only supported if they have been added to that certificate store.

I've tried double clicking on the certificate file and installing the cert, but I am still getting the same issue.

Any ideas what's going on?

Upvotes: 0

Views: 1348

Answers (2)

Vignesh
Vignesh

Reputation: 1882

As Mentioned by @daniel you need to provide Shared User Certificates. After that you need to get certificate from X509Store.

List<Certificate> certificatesViews = new List<Certificate> { };
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
for (int i = 0; i < store.Certificates.Count; i++)
{
   X509Certificate2 cert = store.Certificates[i];
   if(cert.SerialNumber=="YourSerial")
   {
     client.ClientCertificates.Add(cert);
   }
}

Upvotes: 0

Daniel
Daniel

Reputation: 136

Out of sheer desperation, I enabled Shared User Certificates in the capabilities section of my app's package manifest. It worked! Hopefully this helps others!

enter image description here

Upvotes: 1

Related Questions