“File Not Found” When Parsing P8/PEM File to Create JWT Token in Azure S0 Instance – But Works Locally

Posted: August 5, 2025 in Uncategorized

Have you run into a FileNotFoundException when attempting to generate a JWT token using ECDsa.Create()? Well, you’re not alone… This will work locally, but not work in Azure. Especially on certain Azure instance types.

I ran into this issue when attempting to create a JWT token for Apple In-App Purchase API access.

Easy fix, though! Under App Settings in Environment Variables add the following setting:

WEBSITE_LOAD_USER_PROFILE = 1

This is because, even though it’s not reading a file, it throws that error. It’s silly, I know, but hey…

Additional info gleaned from Copilot:

🧠 What’s Likely Happening

  • The ECDsa implementation on Windows relies on CNG (Cryptography Next Generation), which sometimes expects access to user profile directories or system-level key stores—even if you’re passing in a byte array.
  • On S0 App Service plans, the environment may lack access to certain native crypto libraries or user profile loading, causing CngKey.Import() to throw a "File Not Found" error.

✅ Additional Tips

  • Make sure your private key is in PKCS#8 format and uses a supported curve like nistP256.
  • If you’re using .NET 6 or later, consider switching to OpenSSL-based implementations via Linux App Service plans, which tend to be more predictable for ECC operations.
  • For Elastic Premium plans, this issue is even more persistent unless the workaround is applied.

Leave a comment