1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace CMSURLSample
{
class Program
{
static void Main(string[] args)
{
string cmsMethod = "CMSURL";
string url = @"https://host/api/system/groups/1232";
var uri = new Uri(url);
string clientCertificatePath = @"C:\certificates\cert.p12";
string clientCertificatePassword = "test";
//Create X509Certificate from the client certificate provided
var certificate = new X509Certificate2(clientCertificatePath, clientCertificatePassword, X509KeyStorageFlags.PersistKeySet);
Byte[] signingData;
signingData = Encoding.UTF8.GetBytes(uri.AbsolutePath);// Use the absolute path of the URL for signing
// Create a detached digital signature for the message data.
var signedCms = new SignedCms(new ContentInfo(signingData), detached: true);
var signer = new CmsSigner(certificate) { IncludeOption = X509IncludeOption.EndCertOnly };
signer.SignedAttributes.Add(new Pkcs9SigningTime());
signedCms.ComputeSignature(signer); // Compute the signature
Byte[] signature = signedCms.Encode();
// cmsHeader need to be copied to the Authorization HTTP Header for authentication
string cmsHeader = cmsMethod + "`1" + " " + Convert.ToBase64String(signature);
}
}
} |
Partager