UnauthorizedAccessException: Access to the path 'D:\inetpub\wwwroot\ denied

Anjali Agarwal 1,531 Reputation points
2023-03-03T01:12:05.4366667+00:00

I have an application that writes the data in a pdf file. The pdf file exists in wwwRoot directory inside the application. This is how I am accessing the pdf file:

string blankPDF = Path.Combine(_environment.WebRootPath, "BlankPDFFiles");
    string filledPDF = Path.Combine(_environment.WebRootPath, "FilledPDFFiles")

I am getting the blank PDF file from wwwRoot/ BlankPDFFiles and writing to the file and then saving the file to WWWRoot/FilledPDFFiles with a different name using this code:

 string pathToFile = filledPDF + "\\Package" + "_" + employee.EmployeeNumber + ".pdf";
 pdf.Save(pathToFile);

I am using third party tool to write to the pdf file. This is working fine on my local computer, but when I deployed the code to server. I start getting the error saying

UnauthorizedAccessException: Access to the path 'D:\inetpub\wwwroot\Package\wwwroot\FilledPDFFiles\test1234.pdf' is denied.folder FilledPDF exists in wwwRoot so not sure why it is going to D:\inetpub\wwwroot\ rather than going directly to Package\wwwroot. This is my directory structure:

User's image

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 4,721 Reputation points
    2023-03-03T01:48:56.7766667+00:00

    This is working fine on my local computer, but when I deployed the code to server.

    I guess that on your local computer you use Visual Studio to run the application on the IIS Express. If so the worker process to run the application has your user account which you logged in your PC and executed the Visual Studio. Probably your account has an administrator right and has an access right to the wwwroot holder.

    When you deployed the application to the server the account of worker process is the Network Service or AppPoolIdentity by default which has very low access right. That is why you got the UnauthorizedAccessException.

    To solve the issue, therefore, you will have to give the account of worker process a proper access right to the FilledPDFFiles folder.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.