Failure to upload the file in the download host when the site is placed on the server

mohammad ghanezada 1 Reputation point
2022-11-04T09:08:25.573+00:00

I use FTP in Asp.net core to upload files to the host and download them through the following codes
public static void AddImageToServer(this IFormFile image, string fileName, string orginalPath)
{

                string ftp = PathExtension.FtpAddress;  
                var ftpUserName = PathExtension.FtpUsername;  
                var ftpPass = PathExtension.FtpPassword;  
                var ftpFolder = orginalPath;  
  
                 
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);  
                request.Credentials = new NetworkCredential(ftpUserName, ftpPass);  
                request.Method = WebRequestMethods.Ftp.UploadFile;  
                
                using (Stream ftpStream = request.GetRequestStream())  
                {  
                    image.CopyTo(ftpStream);  
                }  
            }  
        }  

But when the image or any type of file is sent to the download host, the following error is displayed UnauthorizedAccessException: Access to the path 'C:\public_html\Images' is denied.

Important note: When the project is run on the local host and we are in the development environment, the upload is done without any problem. But when the site is running on the server, it can no longer communicate with the download host to upload files, and it is really surprising.

We have checked the access to the folders in the download host and all access has been granted. But we face this error again: UnauthorizedAccessException: Access to the path 'C:\public_html\Images' is denied.

For further investigation, I introduced a folder that does not exist in the download host in my code for uploading a file called test. It was interesting . When I ran the site on the local host, it gave an error that the path could not be found, but when I ran my site on the server, it gave this error again.

UnauthorizedAccessException: Access to path 'C:\public_html\test' is denied.

Even though the test folder doesn't exist at all. So now we assume that the problem is not the download, but we don't know where the problem is. please guide me. Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,237 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,036 Reputation points Microsoft Vendor
    2022-11-08T07:03:27.03+00:00

    Hi @mohammad ghanezada ,

    But when the image or any type of file is sent to the download host, the following error is displayed UnauthorizedAccessException: Access to the path 'C:\public_html\Images' is denied.

    For the web application, when host on IIS and remote server, we may be impersonating a user profile, running under a non-standard user account for the application pool (that is, not NETWORK SERVICE) or explicitly writing the file on a thread that’s running on a different user account. So, when access the folder, it might receive the Access to the path 'C:\public_html\Images' is denied error.

    Looking at the user permissions for C: it’s clear that no special permissions have been granted for the web user. Thus, our task is first and foremost to identify the user that’s trying to write the file.

    You can try to use the Process Monitor to show real-time file system, Registry and process/thread activity, then you’ll be able to see the exact user account that tried to perform the denied action. After that you can granting it NTFS write rights to the C: directory.

    For example, I create a MVC application and use the following code to write text to a txt file,

        public IActionResult Privacy()  
        {  
            System.IO.File.WriteAllText(@"C:\Test.txt", "Hello world!");  
            return View();  
        }  
    

    After hosting the application on IIS, when calling the Privacy method, it will show the Access to the path 'C:\Test.txt' is denied error.

    Open the Process Monitor, enable the folder filter and click the cyan funnel icon to open up the filter editor window:

    258181-image.png

    Since we know IIS is running under the w3wp.exe process, we can add a filter that includes all events with a process name of w3wp.exe. As soon as we add an Include filter, all event that do not match an include filter are excluded.

    258151-image.png

    As we can see that, after hosting on IIS, when access the Test.txt file, the user is "IIS APPPOOL\website2".

    Then, we can go to the C drive properties window, and add the IIS APPPOOL\website2 user and granting it NTFS write rights to the C: directory

    258116-image.png

    And finally, we can run the website again and verify that we’ve now got proper permissions for writing the Test.txt file to the C: directory.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments