using Amazon.SecretsManager;
using Amazon.SecretsManager.Model;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
public static byte[] streamToByteArray(Stream input)
MemoryStream ms = new MemoryStream();
public static void Main()
Console.WriteLine("Retrieving the P12 Certificate from Secrets Manager...");
GetSecret("mybinarysecret");
Console.WriteLine("Secret fetched from AWS Secret Manager");
public static void SaveData(string FileName, byte[] Data)
BinaryWriter Writer = null;
Writer = new BinaryWriter(File.OpenWrite(Name));
public static void GetSecret(string secretName)
IAmazonSecretsManager client = new AmazonSecretsManagerClient("AKIA4RS54YVF35CF6VTW", "JluSTT5Bv+y9BRXf8E7XEGBuS33pesr7eddZUaL0", RegionEndpoint.GetBySystemName("us-east-1"));
GetSecretValueRequest request = new GetSecretValueRequest()
VersionStage = "AWSCURRENT"
GetSecretValueResponse response = null;
response = client.GetSecretValueAsync(request).Result;
Console.WriteLine(e.Message);
var outputStream = new MemoryStream(response.SecretBinary.ToArray());
var result = BitConverter.ToString(outputStream.ToArray());
Console.WriteLine(result);
SaveData("MyCertificate.p12",outputStream.ToArray());
X509Certificate2 certificate = new X509Certificate2("MyCertificate.p12", "badssl.com", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
Console.WriteLine("Subject: {0}", certificate.Subject);