Skip to content

Instantly share code, notes, and snippets.

@gabrielbarceloscn
Created March 5, 2019 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielbarceloscn/d4eafa0e7edaad7adf3f69ec731da14e to your computer and use it in GitHub Desktop.
Save gabrielbarceloscn/d4eafa0e7edaad7adf3f69ec731da14e to your computer and use it in GitHub Desktop.
Find and load a X509Certificate2 as an embedded resource as a file in c#
var assembly = Assembly.GetEntryAssembly();
var resourceName = "ravencrt1.pfx2";
var resourcePath = assembly.GetName().Name + "." + resourceName.Replace(" ", "_")
.Replace("\\", ".")
.Replace("/", ".");
using (var resourceStream = assembly.GetManifestResourceStream(resourcePath))
{
if (resourceStream != null)
{
byte[] ba = new byte[resourceStream.Length];
resourceStream.Read(ba, 0, ba.Length);
certificate = new X509Certificate2(ba);
}
}
if (certificate == null)
throw new Exception("Não foram encontrados certificados cliente para o servidor.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment