0

I'm writing an ASP.NET Core app, and I'm trying to use a file's path without exposing it to the web. I set it to copy to output directory, so it loads when I test it on localhost, but it gives me an error when published to the web:

The system cannot find the file specified.

This is the code that generates the path:

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "filename.p12");

I've also tried

Path.Combine(Environment.CurrentDirectory, "Config", "filename.p12");

I'm guessing AppDomain.CurrentDomain.BaseDirectory and Environment.CurrentDirectory point to a different location when published? Or filename.p12's 'Copy to Output Directory' property doesn't carry over to the published version?

EDIT:

I also tried:

Path.Combine(environment.ContentRootPath, "Config", "file.p12");

Where environment is an IHostEnvironment, to the same effect. I'm getting the feeling that the file isn't being copied over into the published version. So, I changed the filename I put in this question to be file.p12 instead of file.txt, which is what I originally put it as, in case it doesn't copy over that type of file for security reasons.

Seldom
  • 11
  • 1
  • 5
  • See if this helps https://stackoverflow.com/questions/10951599/getting-current-directory-in-net-web-application – TheGeneral Aug 31 '20 at 09:21
  • Are you deploying to a Linux environment? – Roar S. Aug 31 '20 at 10:53
  • @RoarS. How can I tell? I think I'm deploying to a Windows environment, since I'm deploying to Microsoft Azure. I'm deploying from a Mac, if that changes anything. – Seldom Aug 31 '20 at 16:56
  • With Azure you can use Kudu and inspect if the file is present in the location – lukaszberwid Aug 31 '20 at 17:01
  • I'm usually working with AWS, tested Azure once, please bear over me if I'm wrong. I believe Azure is all Linux unless you have some special setup with dedicated VMs. Anyway, you'll need to find out. – Roar S. Aug 31 '20 at 17:01

1 Answers1

0

The error said The system cannot find the file specified., but the file was actually there. I was actually missing the X509KeyStorageFlags.MachineKeySet flag when I read the .p12 file. I think the reason it didn't work in production was that the server's current user didn't own the file, so I needed to specify that the machine owns it with that flag.

Seldom
  • 11
  • 1
  • 5