Problem creating folder in C# Asp.Net

Z

Zeb

Hi all

I've developed a shopping cart in C# and to allow moderators to upload
product images, news images and downloadable PDFs, the app creates a
folder for each product. I start out with the following empty folders:

/img/products/
/img/news/
/pdf/

When a new item is uploaded, the app first creates a folder in the
appropriate place and the folder is named using the product or news
ID. The uploaded files can also be deleted later if necessary.

This works fine locally, but won't work on the remote shared server.
When I try to create the folder on the uploaded website I end up with
the following exception:

"Request for the permission of type
'System.Security.Permissions.FileIOPermission, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed"

I'm using DirectoryInfo.Create to create the new folders.

Interestingly, I wrote a quick ASP3 script that creates folders in the
root of the site and this works fine.

I did read a post yesterday that suggests the aspnet client needs read
access to the root of the physical drive (in this case, D:) to be able
to create folders and that most shared servers won't allow this.

Is this really the case, and if it is, what's the best solution? There
must be a way to dynamically create folders on shared hosting
servers ....

Many thanks for any helpful suggestions!
 
J

JE

You have to configure the security on the folder give the ASP.Net
account the necessary permissions.
 
Z

Zeb

You have to configure the security on the folder give the ASP.Net
account the necessary permissions.












- Show quoted text -

What are the necessary permissions though? The ISP seem to think that
the permissions on that folder will allow the creation of new folders
by the aspnet account ..
 
D

Dominick Baier

The ISP has locked down ASP.NET using Partial Trust (the <trust> element
in web.config) - i don't know exactly which permisssions he chose - but by
default you will only have filesystem access to your web directory from ASP.NET.

Besides that - you cannot create subdirectory in the asp.net web directory
- every directory creation will result in an AppDomain recycly and you will
lose stuff like session/cache etc..

I guess you will have to talk to the ISP to change permissions (he most prolly
won't do that) - maybe you have to get a dedicated server.
 
Z

Zeb

The ISP has locked down ASP.NET using Partial Trust (the <trust> element
in web.config) - i don't know exactly which permisssions he chose - but by
default you will only have filesystem access to your web directory from ASP.NET.

Besides that - you cannot create subdirectory in the asp.net web directory
- every directory creation will result in an AppDomain recycly and you will
lose stuff like session/cache etc..

I guess you will have to talk to the ISP to change permissions (he most prolly
won't do that) - maybe you have to get a dedicated server.

-----
Dominick Baier (http://www.leastprivilege.com)












- Show quoted text -

Damn. Thanks for the info Dominick... most helpful as always.

Do you know of any other solutions? The only one I can think of is to
store the images and pdfs as blobs in the database. I was reluctant to
do this, but it seems I may not have a choice.

I find it strange that I can't create a subdirectory though. I've
developed a site for a dedicated server in the past where users could
upload images in the same way, and sub directories were created ...
and session details were not lost. There must be plenty of sites out
there on shared hosting that do the same...

I guess I'll just have to continue testing / talking to the ISP, and
if all else fails I'll just have to use blobs.
 
D

Dominick Baier

The subdir behavior has changed in 2.0 - you can easily try that -

simply create a folder and watch the appdomain shut down...
 
Z

Zeb

The subdir behavior has changed in 2.0 - you can easily try that -

simply create a folder and watch the appdomain shut down...

-----
Dominick Baier (http://www.leastprivilege.com)








- Show quoted text -

That would explain it - the site I mentioned was .net 1.1.

How rubbish. I guess they have their reasons though, so I'll just have
to live with it!

Would you recommend any approach in particular? Are blobs the way to
go?
 
Z

Zeb

The answer!!!

Here's some lateral thinking for you. Rather than create new folders,
I'll simply take the ProductID and file name, hash them into a unique
filename and upload them all to the same folder .. or perhaps file
them in predefined folders such as 0 to 9 (the first digit of the
unique ID).

Job done!

Just thought I'd post this in case anyone was curious as to how I got
around the problem.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top