Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,
Hope you are doing well.

I am using file upload control to upload pdf file in asp.net.
I've one specific document folder under root directory, like ~/PaperDocument.
For each new upload I am creating sub directory under the "PaperDocument".

I am getting error in following piece of code:
C#
public void SaveAttachment(string Year, string FileName)
       {
           if (!Directory.Exists(Server.MapPath("~/PaperDocument/" + Year)))
           {
               Directory.CreateDirectory(Server.MapPath("~/PaperDocument/" + Year));
           }

           fuAttachment.SaveAs(Server.MapPath("~/PaperDocument/" + Year + "/" + FileName));
       }


This code is working on my local but throwing an error on shared hosting server.

CSS
Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

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


I believe that this is the security issue. If I set full control(read/write permission) for Everyone on the hosting Panel then it is working. but I don't want to do this.
How can I permit my application to create folder.

Is anyone have idea about this.
Posted

1 solution

Your application runs on IIS under a given Application Pool.
This application pool owns its own credentials; these are the credentials you need to give permissions to your destination folder to.

Please see SO: IIS AppPoolIdentity and file system write access permissions[^].
 
Share this answer
 
Comments
Herman<T>.Instance 21-Oct-15 3:35am    
good explanation
phil.o 21-Oct-15 3:37am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900