Search code examples
c#azureazure-web-app-serviceazure-webjobsazure-security

How to upload pfx certificate in Azure and access and use it in C#?


In my web job for decryption, I am using a .pfx certificate. But for more security purpose I have to upload this certificate to Azure store and access it through c#.

Can anyone provide more information(links) on this?


Solution

  • According to your description, I assumed that you could follow the steps below for uploading your PFX file and access it from your app.

    Upload the PFX certificate

    Log into portal.azure.com, choose your app service, then click "SETTINGS > SSL certificates", then click Upload Certificate as follows for adding your PFX certificate file:

    enter image description here

    Add app setting

    Click "SETTINGS > Application settings" section of your app service, add an app setting named WEBSITE_LOAD_CERTIFICATES with its value set to the thumbprint of your uploaded PFX certificate file as follows:

    enter image description here

    Access from app

    You could leverage the following code snippet for retrieving the certificate as follows:

    X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    certStore.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certCollection = certStore.Certificates.Find(
                           X509FindType.FindByThumbprint,
                           “{your-cert's-thumbprint}”,
                           false);
    // Get the first cert with the thumbprint
    if (certCollection.Count > 0)
    {
       X509Certificate2 cert = certCollection[0];
       // Use certificate
       Console.WriteLine(cert.FriendlyName);
    }
    certStore.Close();
    

    Additionally, here is a previous blog talking about using certificates in Azure websites applications, you could refer to here.