Access to the path is denied (An exception of type 'System.UnauthorizedAccessException' occurred) after deploying to azure websites

Deadly 提交于 2019-12-10 10:43:32

问题


I am downloading content from blob and storing it in the local folder of the user browsing my application. Everything is working fine without any issues locally but after deploying to App Service Web Application, I am getting access denied issue. I have tried the following options,

Option 1:

string pathString = @"D:\Test";
System.IO.Directory.CreateDirectory(pathString);

I get access denied issue when trying to create the directory after deploying to app service web app.

Option 2:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

When executing locally, it gives me the the path F:\Users\xxxx\Desktop\TestEncrypt - Copy.txt where as after deploying it does not retrieve any path.

Option 3:

System.IO.Path.GetTempPath()

When executing locally, it gives me the following path F:\Users\xxxxx\AppData\Local\Temp\TestEncrypt.txt whereas after deploying to app service web app it give me the following path D:\local\Temp\TestEncrypt.txt I tried creating a directory using GetTempPath but it does not create any folder

Request your valuable inputs in resolving this issue


回答1:


Option 1: string pathString = @"D:\Test";

The reason is that application code uses this identity for basic read-only access to the operating system drive (the D:\ drive).

Reference : Operating system functionality on Azure App Service

Option 2: Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

The desktop folder format is '%systemdrive%\users\%username%\Desktop'. According to Kudu Environment page, we can find that the systemdrive variable value is 'D:'. The username variable value is 'RD0003FF2AE3CC$'. Since Azure Web App runs in a secure environment called a sandbox, the user 'RD0003FF2AE3CC$' is a virtual user which doesn't not exist. To prove it, we can find all the user names from Kudu Debug Console Window. Here are the users I found in Kudu.

D:\Users>dir

Directory of D:\Users

04/01/2017  11:36 PM    <DIR>          .NET v2.0
04/01/2017  11:36 PM    <DIR>          .NET v2.0 Classic
04/01/2017  11:36 PM    <DIR>          Classic .NET AppPool
06/01/2017  07:32 AM    <DIR>          OnStartAdmin
06/01/2017  07:27 AM    <DIR>          Public
06/01/2017  07:48 AM    <DIR>          SiteStorageAdmin

Option 3: System.IO.Path.GetTempPath()

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.

Reference : Azure Web App sandbox

We 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.

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

my requirement is to download the blob content into the users machine

There are 2 ways to implement your requirement.

First, you could get blob content and save it to a memory stream. After that, you could transfer this data to client using following code.

public ActionResult Download()
{
    string blobName = "abc.png";
    string containerName = "mycontainer";
    string connectionString = "";
    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

    // Retrieve reference to a blob named "photo1.jpg".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

    MemoryStream ms = new MemoryStream();
    // Save blob contents to a file.
    blockBlob.DownloadToStream(ms);
    ms.Position = 0;

    return File(ms, "application/octet-stream", blobName);
}

If the blob size is very large, you could generate a URL with SAS and redirect to the URL. The client will download the file directly from the blob server.

public ActionResult Download()
{
    string blobName = "abc.png";
    string containerName = "mycontainer";
    string connectionString = "";

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    var sasConstraints = new SharedAccessBlobPolicy();
    sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
    sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

    var sasBlobToken = blockBlob.GetSharedAccessSignature(sasConstraints);

    var sasUrl = blockBlob.Uri + sasBlobToken;

    return Redirect(sasUrl);
}


来源:https://stackoverflow.com/questions/44415284/access-to-the-path-is-denied-an-exception-of-type-system-unauthorizedaccessexc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!