如何在 Xamarin 上创建证书签名请求

问题描述

我们如何在 Xamarin 平台上生成证书签名请求 (CSR)。

CertificateRequest 可用于桌面 .net,但 Xamarin/mono 平台不支持它,它会抛出 PlatformNotSupportedException。

解决方法

C# 版本的 SandCastle 支持创建证书签名请求 (CSR)

 byte[] CreateSigningRequest(string commonName,RSA rsa)
 {
     var name = new X509Name($"C=NZ,O=MyOrg,L=MyLocation,OU=MyOrgUnit,CN={commonName}");
     var keys = DotNetUtilities.GetKeyPair(rsa);
     Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA256WITHRSA",name,keys.Public,null,keys.Private);
     return csr.GetEncoded();
}