Search code examples
c#azurecertificateembedded-resourcex509certificate2

Loading a certificate as embedded resource in an assembly


I have added a PFX file as an embedded resource in an assembly to use the certificate to sign a JWT token. I load the pfx as stream and read all the bytes and using X509Certificate2 to load the private key

public X509Certificate2(byte[] rawData, string password)

Works fine on dev machine in both Debug/Release, but when deployed to azure apps ervice or a build machine i am seeing "Bad data" error.

Any help greatly appreciated. Thanks


Solution

  • It seems that you’d like to access certificate from app on Azure App Service web app, you can upload your certificate to the certificates collection in Azure Websites and consume it in your web application from your site’s personal certificate store.

    Upload a certificate

    enter image description here Adding an app setting named WEBSITE_LOAD_CERTIFICATES with its value set to the thumbprint of the certificate

    enter image description here

    Consuming certificate in application

    X509Certificate2 retVal = null;
    
    X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    certStore.Open(OpenFlags.ReadOnly);
    
    X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
    
    if (certCollection.Count > 0)
    {
        retVal = certCollection[0];
    }
    
    certStore.Close();
    

    For detailed information, please check: Using Certificates in Azure Websites Applications