Jayesh Tanna
Jayesh Tanna

Reputation: 418

Azure + ASP.Net + System.UnauthorizedAccessException: Access to the path '/Content/img/CourseImages' is denied

I am trying to upload an file using asp.net on azure (its not VM). But constantly, i am receiving below error:

System.UnauthorizedAccessException: Access to the path '/Content/img/CourseImages' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
at System.IO.Directory.CreateDirectory(String path)

Here is my code snippet:

string courseImageFolderPath = ConfigurationManager.AppSettings["CourseImage"];
string courseImageFilePath = Path.Combine(courseImageFolderPath, fileName);
if (!Directory.Exists(courseImageFolderPath))
        Directory.CreateDirectory(courseImageFolderPath);
courseImage.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(courseImageFilePath));

Upvotes: 0

Views: 2581

Answers (1)

Joey Cai
Joey Cai

Reputation: 20067

Every Azure Web App has a local directory(D:\local) which is temporary. The content in this folder will be deleted when the run is no longer running on the VM. This directory is a place to store temporary data for the application. It is not recommended to use this folder by your web application.

According to Azure Web App sandbox, I suggest you create a temp folder at the root of your web application folder(D:\home\site\wwwroot) and use it to store the temp data. Or as jayendran said, you could use blob storage to upload your image.

string tempFolder = Server.MapPath("~/TEMP");
if (!Directory.Exists(tempFolder))
{
    Directory.CreateDirectory(tempFolder);
}

For more detials, you could refer to this issue.

Also, it seems that the app service do not get access to network resource. So what you would do is impersonate a user that has access on the network resource in order to create the directory. Please refer to this article.

Upvotes: 1

Related Questions