ASP.NET Core System.UnauthorizedAccessException: Access to the path 'C:\..." is denied

13,381

Solution 1

In my case (IIS 8.5), I had to add the IIS_IUSRS to the application folder to resolve this issue

Solution 2

Try checking the user that is running the application pool (instructions here: https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities).

If that user doesn't have access to the C drive, the write will fail.

(Also: try not to code the path again and again in your code. Declare C:/HomeNET/Houses/ as a const and refer to it that way, or better still, read it from the web.config)

Share:
13,381
koeks525
Author by

koeks525

Updated on June 14, 2022

Comments

  • koeks525
    koeks525 almost 2 years

    I am new to ASP.NET Core so I am playing around with making an API. I am trying to upload a picture through my Web API. The picture is to be saved onto the web server. I am running the API on a Windows Server 2016 Standard Edition VPS Machine. I have IIS 10 installed on the machine. The error I get back (when remote debugging):

    {System.UnauthorizedAccessException: Access to the path 'C:\HomeNET\Houses\13' is denied. at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode) at HomeNetAPI.Controllers.HouseController.d__6.MoveNext() in C:\Users\okuhl\Documents\HomeNet\Web API\HomeNetAPI\src\HomeNetAPI\Controllers\HouseController.cs:line 83}

    This is weird because directories are created, should they not exist. Here is some of the code, where the fail happens:

    if (resultHouse != null)
    {
        if (!Directory.Exists($"C:/HomeNET/Houses/{resultHouse.HouseID}"))
        {
            Directory.CreateDirectory($"C:/HomeNET/Houses/{resultHouse.HouseID}");
        }
    
        String fileName = new FileInfo(imageFile.FileName).Name;
        String fileExtension = new FileInfo(imageFile.FileName).Extension;
        using (var fileStream = new FileStream($"C:/HomeNET/Houses/{resultHouse.HouseID}", 
            FileMode.Create)) //Bombs out after this line
        {
            var result = imageFile.CopyToAsync(fileStream);
            if (result.IsCompleted)
            {
                newHouse.HouseImage = Path.Combine($"C:/HomeNET/Houses/{resultHouse.HouseID}", $".{fileExtension}");
                var updateResult = await Task.Run(() =>
                {
                    return houseRepository.UpdateHouse(newHouse);
                });
                if (updateResult != null)
                {
                    response.DidError = false;
                    response.Message = $"House {newHouse.Name} has been created successfully! Invite friends ad family to your house!";
                    response.Model = newHouse;
                    return Ok(response);
                }
                else
                {
                    response.DidError = true;
                    response.Message = "Something went wrong with creating the house. Please try again";
                    response.Model = newHouse;
                    return BadRequest(response);
                }
            }
            else
            {
                response.DidError = true;
                response.Message = $"Something Went wrong with creating your house. House image could not be saved onto the system. \n {result.Exception.Message}";
                response.Model = newHouse;
                return BadRequest(response);
            }
        }
    }
    

    I have tried making the folder available to everyone and that didn't work. I also tried giving my AppPool permission to modify the folder and that didn't work.