Azure and edit or delete deployed files from your website

by ingvar 21. december 2010 09:32

If you change the file permission on deployed files in the WebRole, you will be able to edit or delete those files from you website. This includes the web.config file!
Also if you write new files in the WebRole, you also need to change the file permission for those files.

My colleague at Composite Marcus Wendt found the code for changing the file permission here. Ill restate the code below. This code is not enough, you also need to edit the ServiceDefinition.csdef file. You need to add '<Runtime executionContext="elevated" />' to the ServiceDefinition.csdef file to give your WebRole rights to change file permissions:


<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AzureTest"
                   xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole">
    
    <Runtime executionContext="elevated" />

  </WebRole>
</ServiceDefinition>

And here is the code for changing the permission for a given file:

void ChangePermission(string filePath)
{
    SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

    IdentityReference act = sid.Translate(typeof(NTAccount));

    FileSecurity sec = File.GetAccessControl(filePath);
    sec.AddAccessRule(new FileSystemAccessRule(act, FileSystemRights.FullControl,
                                               AccessControlType.Allow));

    File.SetAccessControl(filePath, sec);
}

Tags:

.NET | Azure | C#

Comments (2) -

Dalibor
Dalibor Czech Republic
07-01-2011 09:57:37 #

The problem with such approach is that after a VM gets recycled from any reason, it is rebooted from origial deployment file which doesn't contain file changes you had made. An app may get recycled at any time and you loose any changes you made on local FS.

ingvar
ingvar Denmark
07-01-2011 10:06:20 #

@Dalibor You are correct! In the future I'll do a blob post on how to solve this issue, so stay tuned Smile

Pingbacks and trackbacks (1)+

About the author

Martin Ingvar Kofoed Jensen

Architect and Senior Developer at Composite on the open source project Composite C1 - C#/4.0, LINQ, Azure, Parallel and much more!

Follow me on Twitter

Read more about me here.

Read press and buzz about my work and me here.

Stack Overflow

Month List