I need to write files into a folder using c# that is outside of root folder on IIS 7.
I have made the folder and given IIS_IUSRS and the app pool users all rights on the folder but I always get UnauthorizedAccessException when I try to write to it.
Everything I googled says the solution is virtual directory, but I cant have the folder emptied every time I publish the web.
Is there a solution to this?
You also need to grant file access permissions to the NETWORK SERVICE account.
Related
I'm testing a web app locally that will be hosted in a Linux environment. For testing, I created a symlink folder using the administrator command-line and mklink /D. This link is set inside the wwwroot folder of my app and is linked to a directory in C:\Users\User\the_folder. Reason for doing this is because I will need to create a symlink on my linux server to a mounted block storage drive.
I'm able to write files and new directories to this symlink directory, but for some reason it won't allow me to delete a file. I AM, however, able to delete an entire directory along with its contents with System.IO.Directory.Delete(path, true);
To delete a file, I'm trying to use System.IO.File.Delete, but this returns an access denied error.
Does anyone know what the problem might be?
I have a VPS (Cloud Server) and I have Visual Studio, IIS, SQL... all those nice things installed. I am also sometimes doing development on this VPS.
This website is basically a simple file manager. When it loads, it lists files and folders that resides in the App_Data folder. This said, I am using the App_Data as my "root/home/topmost-directory" for the file manager.
The reason I am putting them in the App_Data folder is because I don't want users to just go like this: www.mywebsite.com/ROOT/[gets a list of files].
Instead, if they try to go: www.mywebsite.com/App_Data/[tries to get list], IIS will block them with a nice error (because IIS blocks access to that folder).
So, the website accesses the structure in the App_Data folder via code behind. This is how the website navigates. It can download, upload, move files around, rename... delete... everything right to/inside the App_Data folder.
The only problem is, this only works when I run the website in Visual Studio and run my tests, testing the website, debugging, etc. The moment I publish (via File System in Visual Studio) the website to the directory: "C:/inetpub/Sites/[WebsiteFolder]", and navigates to the website's URL, the website does not work any more. It loads, but it does not get the file structure in the App_Data folder any more, it cannot create new folders, cannot rename them...
This website works fine when I upload it via FTP to another webs space that is hosted somewhere else at another hosting company (which is shared hosting).
So I guess (my best guess) the IIS on my VPS does not allow this kind of access. And I have been trying to find settings on IIS somewhere that would/could allow this.
I have enabled directory browsing, but that's probably the only thing I can think of other than making sure the directory is accessible (readable/writable).
Do you perhaps know what else I can try?
Ohkay, so this was a very silly mistake - but that's how we learn!
The App_Data folder did not have any other files or folders inside of it.
And as we know, ASP.NET does not publish empty directories. This means those empty folders never existed once I published the website.
There are 3 ways to fix this that I know of at the moment:
If your solution has a publish profile (so that you can publish the site), you can right click the empty folder in Visual Studio, and say Publish [FolderName] eg:
You can just create an empty file in that directory, and publish your whole website again. Because the folder is not empty any more, it publishes with the rest of the website.
Just manually create the folder on your hosting space.
Thanx for everyone's input!
Greatly appreciated!
I have an ASP.Net website and a C# console application (both developed in VS2010). Both upload files to our web server (Windows Server 2003/IIS 6.0). Users logging in to the website can then view and download these files.
This works fine in website. But when I try to download files uploaded by the console application, I get the error:
UnauthorizedAccessException
Access to the path 'C:\xxx\yyy\zzz\abcd_20120309.pdf' is denied.
I have already manually provided "Full Control" to NETWORK SERVICE account for that folder.
Why is Asp.Net code unable to access a folder created by the console application?
Any idea?
Thanks!
You need to provide access to the folder for IIS_IUSRS (or something along this line).
AKA you need access for IIS.
Including access to everyone, doesnt include this user. So it must be done directly.
Network service will not suffice.
If IIS_IUSRS doesnt appear in the list, search for it.
Don't know if it matters in this case but one thing I have seen in the past is:
The application first saves the file in a temporary folder and then moves it to the target folder.
Sometimes, don't know the exact rules, the file access right follows the file in the move. i.e. the file ends up with the access rights for the temporary folder even in target folder.
Check the access rights on the file and see if it differs from the target folder.
string windowsDirectory = Environment.GetEnvironmentVariable("ProgramFiles");
string mydirecoty = windowsDirectory + "\\" + "NetServices\\";
if (!Directory.Exists(mydirecoty))
{
Directory.CreateDirectory(mydirecoty); //Access to the path 'C:\Program Files (x86)\NetServices\' is denied.
}
exception is thrown when I run my .net application without Administrator account. While If I try to create directory in other then C:\ drive like D:\ it runs fine.
What is solution that I want to create directory in Program Files/ Windows folder?
The C:\program files folder is protected (by design) in Vista and Windows 7 (and Windows Server 2008 / 2008 R2) - normal user accounts do not have any permission to create directories in there - it's a system folder.
Either you need to run as admin - then you have permission to create directories even in protected system folders - or you create the directories elsewhere and not inside a protected system folder. The second option would be the recommended and preferred option.
If your application is intended to be run by the user, write to the user's app data folder or temp folder. Your app should install to the Program Files directory, and perhaps use some files in the Windows directory, but it should never have to write to either location.
You can easily get the user's app daa folder from the environment variables.
if you are running this from web application make sure that the application pool user has ntfs permission of that folder.
You need to elevate user privileges as it is a feature in windows vista/7.
Running as admin will solve the problem.
if you use operating systems higher than Windows XP, you will not able to access C: \ PrpgramFiles protecded because reading and writing, unless AVII your application with Administrator rights.
Bye
I just want to delete the Directory which is in the project folder.
That folder having all web sharing authority and permission.
The problem is arising when i am deleting the folder.
Folder is deleted from the serverpath(Virtual Directory).
But my problem is that when i complete my task and click on any control it will redirect me to the Login page with the return url as there is a secure authentication on the root directory is available
Application domain recycled when
Sub-Directories are deleted, that's why your session will lost and you are redirected to the login page.
For more details, check this article from MSDN Blog and read Why does an application domain recycle? from here
ASP.NET Case Study: Lost session variables and appdomain recycles
and also check this one Deleting ASP.NET 2.0 Application Sub-Directories Shuts Down the AppDomain
As noted in another answer, this is because the web app is restarted whenever you delete a folder inside the web app's directory structure (i.e. below the web app's root directory).
The only solution I found for this problem is to move the data directories (which you create/delete/modify) outside the web app's root directory / virtual directory.
Then we create a link (junction) in the file system so that the directory appears to be inside the virtual directory. This prevents ASP.NET from monitoring the data directory for delete operations.
Example:
Our web site (virtual directory) is located at C:\projectX\website
the data directory (where we create/delete files and folders) is located at C:\projectX\data
then we create a link which makes the data folder available as C:\projectX\website\data
The link is created using the program Linkd.exe (available in the windows resource kit), with the following command:
linkd c:\projectX\website\data c:\projectX\data
Now C:\projectX\website\data is a link/junction which points to the real data directory. Inside your web app, you can continue working as if the data directory were a physical directory below the web app's root directory.
E.g. in your web site you can access the data folder using this code:
Server.MapPath("~/data")
And you can also used the windows file explorer and browse to C:\projectX\website\data. It appears just like a real directory.
As you can see, you can continue to use the linked data folder as if it were a normal folder inside the web app's directory. The only difference is that ASP.NET will not track the directory for delete operations and will therefore not restart the application. This means, you can now create/delete/modify folders and files inside the ~/data directory as you wish, without having the web app restarted.
Can you replace folder from the project root into the App_Data folder?