如何发送带有 UWP 应用程序附加证书的 RestSharp 请求?
How to send a RestSharp request with a certificate attached in a UWP application?
我创建了一个 .NET Core 控制台应用程序来执行我需要的:发送带有附加证书的 RestSharp 请求。一切正常。这是代码:
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);
}
但是当我将相同的代码放入 UWP 应用程序时,它不起作用。我收到此错误消息:
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.
我已经尝试双击证书文件并安装证书,但我仍然遇到同样的问题。
有什么想法吗?
出于绝望,我在我的应用程序包清单的功能部分启用了 Shared User Certificates
。有效!希望这对其他人有帮助!
如@daniel 所述,您需要提供 Shared User Certificates
。之后你需要从 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);
}
}
我创建了一个 .NET Core 控制台应用程序来执行我需要的:发送带有附加证书的 RestSharp 请求。一切正常。这是代码:
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);
}
但是当我将相同的代码放入 UWP 应用程序时,它不起作用。我收到此错误消息:
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.
我已经尝试双击证书文件并安装证书,但我仍然遇到同样的问题。
有什么想法吗?
出于绝望,我在我的应用程序包清单的功能部分启用了 Shared User Certificates
。有效!希望这对其他人有帮助!
如@daniel 所述,您需要提供 Shared User Certificates
。之后你需要从 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);
}
}