Why i cannot write a file, even i add a permission to that file.

U

Umut Tezduyar

My web site user should change some files in the root folder. Why the
following code is not working.
I dont want to manually give permission for the asp.net account for each
file. Instead, i want to give permission that the caller can write to the
specified file.
I have investigated the msdn and web. Is this information is hidden ?? Can
any one help me about this.

string path = System.Web.HttpContext.Current.Server.MapPath(string.Empty) +
@"\";

string file = path + this.insertedcontrolName;

// Enable write permission for the file.

FileIOPermission fileIOPerm1 = new
FileIOPermission(FileIOPermissionAccess.AllAccess, file);

fileIOPerm1.AddPathList(FileIOPermissionAccess.Write, path);

fileIOPerm1.AddPathList(FileIOPermissionAccess.Read, path);

fileIOPerm1.Assert ();

// Writing to the file

TextWriter writer = new System.IO.StreamWriter (file, false,
System.Text.Encoding.UTF8);

writer.Write (this.html);

writer.Close ();
 
S

Steven Cheng[MSFT]

Hi Umut,

Thanks for your posting. As for the file permission problem you mentioned,
are you got the "Access denied" error? In fact, this problem is caused by
the windows's file access premission rather than the .NET's CAS. So setting
the

FileIOPermission fileIOPerm1 = new
FileIOPermission(FileIOPermissionAccess.AllAccess, file);

is not necessary. We need to grant the NTFS's file access permission(
write/modify) of that file to the asp.net 's worker process account( the
machine\aspnet on WIN2K IIS 5 or the NetworkService on win2k3 iis6). After
that, you can get your page write/modify the file correctly.

Hope helps. If you still have anything unclear, please feel free to post
here. Thanks.'

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
U

Umut Tezduyar

This is not solving my problem. I have developed a control and i want my
control to write some files in the web applications root.
Do i have to inform users, which will use my control, as add, a permission
to the each file that my application will use.
What if my user deploys its web site with my control to a server that he
cannot change the permission of the files.
 
S

Steven Cheng[MSFT]

Hi Umut,

Thanks for your response. The problem is that the file access protection is
the buildin security protection of the NTFS FILE SYSTEM. If you do want to
provide such permission for multi-user controls, one possbile approach is
to add a sub folder under the web application's root folder ( the VIRUTAL
DIR) and grant the write/modify permission of that folder to the asp.net
worker process account. Then we can put all your usercontrols that need
modify under that sub folder.

BTW, I still recommend that we don't modify the page or usercontrol's
source( aspx or ascx or code behind) at runtime. Because the web
application has the "numRecompilesBeforeAppRestart" setting which indicate
that the web app will be restarted when the number of the asp.net component
files be modified and recompiled has exceed this value, see the following
doc:

#<compilation> Element
http://msdn.microsoft.com/library/en-us/cpgenref/html/gngrfCompilationSectio
n.asp?frame=true

So, I don't think modify the page/usercontrol source is a good means. Would
you consider putting the UserControl's UI change logic into the control's
code so as to make it output different content at runtime? For example, use
some properties to control the UserControl's output and we can set such
properties in Usercontrol's construct. If you have any other ideas or
concerns , please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top