0

I have a certificate file in my Azure Function (.NET Core 3.1) solution that resides at this location in my solution: C:\Users\me\Documents\Code\Test\TestSolution\Test.Subproject\Certificates\cert.p12

The file is set to Copy if newer and Build Action is Content.

I reference the file like this in my code:

_client.Certificates.Add(new Certificate("Certificates/cert.p12", "ssl_094243"));

It works fine in the Azure Function Emulator but when deployed to Azure it can't find the file. In my publish output the folder structure is flattened somewhat so now the file is found at:

C:\Users\me\Downloads\Test.Functions.zip\Content\D_C\a\1\s\Test\Test.Functions\obj\Release\netcoreapp3.1\PubTmp\Out\Certificates\cert.p12

How can I reference this file in my code? I think I need the application route. I tried the code below but it didn't work unfortunately.

_client.Certificates.Add(new Certificate(Path.Combine(Environment.CurrentDirectory, "Certificates/cert.p12"), "ssl_094243"));

James Mundy
  • 4,180
  • 6
  • 35
  • 58
  • 1
    Probably safer to add it as an embedded resource, or even better because it's a certificate, put it in Azure KeyVault. – DavidG Jan 27 '21 at 22:42
  • Hi James, would mind accept my answer for others to refer if it helps? – Doris Lv Jan 28 '21 at 02:22
  • Thanks for the answers, @DorisLv your answer doesn't quite answer my question though. My file is deployed to Azure with my application but I can't work out how to reference that file from my code. – James Mundy Jan 28 '21 at 09:08
  • As your description, your file is not deployed to the wwwroot folder, try use my solution and reference it like how you use locally. – Doris Lv Jan 29 '21 at 01:31
  • @DorisLv I'm not sure my issue is clear: I have done the steps you say but my question is how do I reference that file location it is deployed to? – James Mundy Jan 29 '21 at 09:11
  • Now the cert file is under your project, you could use relative path. @JamesMundy – Doris Lv Jan 29 '21 at 09:33

2 Answers2

0

Just like DavidG said, the safer way to reference cetificate is using Azure key vault.

It is also achievable to use it directly like what you said. Not sure the deployment method you use, consume using Visual studio,click the certificate file you could see properties setting like below, choose Copy always or Copy if new option.

enter image description here

Then check the .csproj file: enter image description here

Redeploy to Azure app service, you could see the file in kudu site: enter image description here

If it is not effective, right click the cert file and deploy manually: enter image description here

Finally you could use it like how you use locally.

Doris Lv
  • 3,083
  • 1
  • 5
  • 14
0

After trying a few things I decided to move the certificate from a sub project to the project I am building and publishing. I then used this code to find out:

string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string certLocation = Path.GetFullPath(Path.Combine(directoryName, "..", "Certificates/cert.p12"));
            _client.Certificates.Add(new Certificate(certLocation, "password"));

What I found was if I kept the certificate folder and file in the sub project it would be copied to the the bin folder in Debug but when publishing it would be saved to the root folder, very strange. Moving the folder to the project I was building meant the certificate was always outputted to the same location. I'm wondering if this is a bug?

James Mundy
  • 4,180
  • 6
  • 35
  • 58