C# MVC Access to path denied when trying to write file - c#

I have an MVC application in which users are able to upload files. Before I write the uploaded file, I create a directory according to date time. I start off with C:\ApplicationName and end up with C:\ApplicationName\20111001\Filename.ext when the upload is completed (in theory).
My problem on my local Windows 7 machine is that I can not write the file.
I get an "access denied" exception no matter which user I give full access to the directory. The strange thing is that the date directory gets created just fine.
I have given the following users full access:
[Current logged in user]
NETWORK SERVICE
IUSR
IIS_IUSRS
Guests
Everyone
Without any success. I really don't understand what is going on here. When I give Everyone full access, I should be able to create a file right?
PS: I use Visual Studio 2010 and ASP.NET Development Server straight out of the box.

I am not running IIS, I am running the watered down version "ASP.NET
Development Server". So I am quite limited
The problem is that in order for you to write to the file directory from the application you will need to run Visual Studio as Administrator.
Windows 7 is preventing the process from going outside of its sandbox because it is running with limited privileges. This is true even if your account is the administrator.

Check the permissions of the parent folder and make sure they are inheritable, you can check this on the advance options window.

This might help a bit... probably application pool permission is the culprit here:
IIS AppPoolIdentity and file system write access permissions

Just had the same problem myself. By default IIS7 AppPools use AppPoolIdentity. Just open up your AppPools in IIS Management Console, select the one you are having problems with, choose Advanced Settings and under Process Model change Indentity to Built-in Acoount > NetworkService.
Since you have already granted NETWORK SERVICE acces to your folder everything should work.

Related

visual studio unable to start program access is denied

I'm getting "Unable to start program http://localhost:[port] Access is denied." when I try to debug an ASP.NET MVC app. Here are some more details, some of which may not be relevant.
This is happening under a somewhat different context for this project. I recently obtained an elevated account on the network, so now I'm running VS 2017 (15.5.4) as Admin every time I start it so that I can access the TFS server. Previous to this, I had been working offline with my non-elevated account and passing code changes to another developer for him to check in to TFS. I am still logged on to the local PC (Windows 10) with my non-elevated account. I just start VS as an admin and when prompted enter the credentials for the elevated account.
This morning was the first time I ran VS under this new context, got the latest code, and tried to do some work. I had to delete an existing TFS workspace, but I did get a complete refresh of the code from TFS.
The first time I went to F5 the code in this new context I get a warning about the app using SSL and VS wanting to self-sign a certificate. I said yes. I forget exactly what happened at that point, I think the browser (Firefox) timed out trying to connect. Anyway, I tried a different browser (Chrome) and still no luck.
Yet now, whether I , +, , or , I very quickly get the message from above "Unable to start program..." I've done a lot of searching on this problem, but have had no success in getting close to a solution. Here's what I've tried:
Cleaned the build and done a fresh rebuild. Building works, no issues.
Use netsh to look for (and delete) URL reservation. There wasn't one.
Tried using a different port.
Deleted temporary asp.net files.
Turn JavaScript debugging option off in VS.
Crtl+F5, w/o debugger.
Turn SSL off for the project.
Checked, Windows Authentication is on in web.config.
Used Process Monitor to look for any "access denied" for either devenv.exe or iisexpress.exe
So what I'm hoping for here is not so much a silver bullet (though that would be great), but more some help in troubleshooting. Access denied means some resource--a file, a registry setting, a network share, something--is inaccessible in the context in which I'm running. Exactly what that is has got to be recorded somewhere, I hope. What log files does VS write to when you hit ? I've looked at the output during build and there's nothing there.
I'm sure I haven't exhausted all resources in troubleshooting this, I just need some help on where to look. Also, I had to leave that project/site and head to another, so I won't be able to try any ideas you all throw at me until tomorrow, at the earliest.
Please take a look at these answers in a similar question. From the list you provided it appears some of these may be new things to try for your case.
Specifically, I have run across the following answers in that linked question that have worked for me at various times in my career:
If you're using Anonymous Authentication, IIS 7 will use the "IUSR"
account for directory access (unless you've set it to use the app
pool identity. If you haven't granted IUSR sufficient permissions on
your site's folders and files, you'll get this error
For me in windows 7 it started to work only after I gave 'Read &
execute', 'List folder contents', 'Read' permissions to site folder
for both users
IUSR
NETWORK SERVICE
I had the same problem, I enabled "Anonymous Authentication" but it
still did not work. So I also ENABLED "Forms Authentication" Then it
worked without any problems. (also try windows auth)
For me, nothing worked except the following, which solved the
problem: open IIS, select the site, open Authentication (in the IIS
section), right click Anonymous Authentication and select Edit,
select Application Pool Identity.
I gave access to "IIS_IUser" but instead it should be "IUSR". That
solved the problem.
I have a very similar problem with yours and my architecture uses windows authentication and what works for me is selecting my project and going to properties. Then select "Enabled" on Windows Authentication.

cann't write a file in server side location(C drive or D drive)using asp.net?

I am using ASP.NET for my web application. I am able to write a text file in server path using this method Server.MapPath(). But I also want to create a text file in different location like C:\ or D:\. I am using IIS version 5.1.
This is my code to write a file in the C:\ drive.
string text="Welcome";
StreamWriter sw = new StreamWriter("C:\\sample.txt", true);
sw.WriteLine(text);
sw.Close();
When I run the application it throws a runtime exception saying
UnauthorizedAccessException. Access to Path C:\sample.txt is denied.
Please guide me how to write a text file in the C:\ drive...
Its obvious that you need permission to be able to create/write/delete etc a file on any location on disk.
The tip here is that your asp.net application is running under one pool, and the pool is running under specific user - this user must have the correct permissions.
Update
There is two kind of users that "runs" together on an asp.net page. The one have to do with permission with IIS, and this is the one that KBoek speak about. For example, a user request a page , from IIS, and the IIS using the USER A to see if can read this file and send it back to web - or have permissions to run.
The second type of user is the pool. From the moment that IIS have permissions to run the pool then run it using the pool user. So if the file is going to read by pool the pool must have permission to read and not the iis.
Why there are two kind of users here. Because the one is represent the user that see the data, and the other represent the programming that we made.
If for example we have an automation tool that make delete to a set of files, then we do not won to the remote client to direct access this file and maybe delete if he like to - but only we must have this permission to do only on programming. So our asp.net program have different permissions from the user that connect and see page, so we have two kind of users here.
The server process runs under a specific user (dependent on your IIS version), that user has to have write access to the folder where you want to save the file. Tell me your IIS version, then give me a minute and I'll look up the user name for you.
In order to write into a certain location, the web server user must have write access granted to that location. You have to set up the folder rights to this for work. The exact user depends on your IIS version.
First of all I'd use Server.Mappath("C:\\sample.txt");.
Secondly, the exception says it all, the user attempting to write a file in the server does not have the permission to write.
Check Checking and Setting up the Correct Permissions on the IIS Server for permissions
Find your user and allow it to write.

Issue with permissions when executing a File.Exists through asp.net site

I've got a script that checks if a file exists. The path is a network share that exists on the server..
I.e. the directory is D:\Mainfolder\Subfolder\file.txt
the network share is \Server\Subfolder\file.txt
The file exists, however from my asp.net site, it says that it doesn't.. I assume that this is a permissions issue since it works if the file is in a different folder.
I've added a bunch of accounts to the permissions of both the share and the folder, but nothing is working..
Is there a way to display which user the File.Exists is being executed as?
Thanks
The user that is accessing the files is the user configured on the application pool of the ASP.NET site or virtual directory. By default, this user will not have access to many local folders/files, let a lone a network share. You have two options:
Change the user configured for the application pool to a user that has access to not only the local files necessary to run the site, but also has access to the network share.
Configure impersonation in the Web.config. See this other SO question here which has a snippet of configuration from Web.config for impersonation.
Some links:
IIS6 Application Pool Identities
IIS7 Application Pool Identities
Starting with Vista a Windows Service is not allowed to access "Desktop things" - one of these being a mounted drive letter aka network share...
You could mess around with the permissions etc. but even if you get it to work this is not supported...
What is the exact goal ? Perhaps there is some other way...
It's probably going to be the user that your application pool is running under. You can see the list of application pools in IIS manager.
However, you can also use Process Monitor to see what users are trying to access the files on your server (including attempts that fail due to insufficient permissions). You can find it at http://technet.microsoft.com/en-us/sysinternals/bb896645. You'll probably need to run this on the remote server where the file actually exists.
Another thing to consider is that while the D:\ drive might be mapped as a network drive when you log in interactively with your account, it probably doesn't exist when the website user is 'logged in'. You'd be better off telling the website to use the UNC path (\server\subfolder...) rather than the D:\ path.
As others have mentioned, your application is probably running under the Application Pool identity. Unless you've changed it explicitly, this account will not show up in your list of accounts to configure.
Getting that sorted out isn't going to help you, though, if you are attempting to access a resource though a mapped drive, as the mapped drive exists only within the scope of the logged-in user.
Think of it this way: you and a colleague share a machine, and you map drive D:\ to \serverA\Shared\Matt, your colleague isn't going to log in and have drive D:\ mapped to your share. She can freely map drive D:\ to \serverX\Secret\Resources.
Start by making sure that you are using a UNC path, then work through the web of permissions issues.

FileUpload error in the server

hi
I have a FileUpload and I save a picture and this is the error
Access to the path 'D:\Hosting\0000000\html\images\APgt_logo.jpg' is denied.
an my local is working well
whats the problem?
this is the code
new_row["Product_imag"] = FileUpload1.FileName.ToString();
FileUpload1.SaveAs(Server.MapPath("/images/"+ FileUpload1.FileName.ToString()));
The user that is running the Web Service process (ie w3wp.exe) doesn't have sufficient rights on the folder you are trying to write to.
Either you have to set write rights in IIS, or you have to adjust the security in the file system. Or both.
You need to provide more information if you want help with that.
This is a permissions problem on your web server.
When you run the project locally, the local web server is executing using your permissions which has write access to the directory in question.
When running on the server, the user the app pool is executing under does not have permission to write to the directory. This is normal as it usually shouldn't.
You might ask this over at serverfault.com to get some good recommendations on how to do this in a secure manner.

Redeploying an ASP.NET site in IIS7 without files in use interfering

We've got a process currently which causes ASP.NET websites to be redeployed. The code is itself an ASP.NET application. The current method, which has worked for quite a while, is simply to loop over all the files in one folder and copy them over the top of the files in the webroot.
The problem that's arisen is that occasionally files end up being in use and hence can't be copied over. This has in the past been intermittent to the point it didn't matter but on some of our higher traffic sites it happens the majority of the time now.
I'm wondering if anyone has a workaround or alternative approach to this that I haven't thought of. Currently my ideas are:
Simply retry each file until it works. That's going to cause errors for a short time though which isn't really that good.
Deploy to a new folder and update IIS's webroot to the new folder. I'm not sure how to do this short of running the application as an administrator and running batch files, which is very untidy.
Does anyone know what the best way to do this is, or if it's possible to do #2 without running the publishing application as a user who has admin access (Willing to grant it special privileges, but I'd prefer to stop short of administrator)?
Edit
Clarification of infrastructure... We have 2 IIS 7 webservers in an NLB running their webroots off a shared NAS (To be more clear, they're using the exact same webroot on the NAS). We do a lot of deploys, to the point where any approach we can't automate really won't be viable.
What you need to do is temporary stop IIS from processing any incoming requests for that app, so you can copy the new files and then start it again. This will lead to a small downtime for your clients, but unless your website is mission critical, that shouldn't be that big of a problem.
ASP.NET has a feature that targets exactly this scenario. Basically, it boils down to temporarily creating a file named App_Offline.htm in the root of your webapp. Once the file is there, IIS will takedown the worker process for you app and unload any files in use. Once you copy over your files, you can delete the App_Offline.htm file and IIS will happily start churning again.
Note that while that file is there, IIS will serve its content as a response to any requests to your webapp. So be careful what you put in the file. :-)
Another solution is IIS Programmatic Administration.
Then you can copy your new/updated web to an alternative directory then switch the IIS root of your webapp to this alternative directory. Then you don't matter if files are locked in the original root. This a good solution for website availability.
However it requires some permission tuning...
You can do it via ADSI or WMI for IIS 6 or Microsoft.Web.Administration for IIS 7.
About your 2., note that WMI don't require administrator privileges as ADSI do. You can configure rights by objects. Check your WMI console (mmc).
Since you're already load balancing between 2 web servers, you can:
In the load balancer, take web server A offline, so only web server B is in use.
Deploy the updated site to web server A.
(As a bonus, you can do an extra test pass on web server A before it goes into production.)
In the load balancer, take B offline and put A online, so only web server A is in use.
Deploy the updated site to web server B.
(As a bonus, you can do an extra test pass on web server B before it goes into production.)
In the load balancer, put B back online. Now both web servers are upgraded and back in use in production.
List item
You could also try to modify the timestamp of web.config in the root folder before attempting to copy the files. This will unload the application and free used files.
Unless you're manually opening a handle to a file on your web server, IIS won't keep locks on your files.
Try shutting down other services that might be locking your files. Some examples of common services that do just that:
Windows Search
Google Desktop Search
Windows Backup
any other anti-virus or indexing software
We had the same server (2003) and the same problem. Certain dll's were being locked and putting the App_Offline.htm in the website root did jack diddly for us.
Solution:
File permissions!
We were using a web service which runs under the Network Service account or the IIS_WPG account to deploy updates to the web site. Thus it needed write access to all the files. I already knew this, and had already set the permissions on the directory a while ago. But for some strange reason, the necessary permissions were not set on this one problem dll. You should check the permissions not only on the directory, but on the problem file as well.
We gave Network Service and IIS_WPG users read/write access to the entire web root directory and that solved our file in use, file locked, timeout, and access denied issues.

Categories

Resources