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

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.

Related

Accessing a VFS shared network Drive through ASP Website and Viewing the files inside, Possible IIS fault

I have an ASP.NET website, that has to access a shared network drive. The shared network drive is on an EMC path or ECS. The management module for the shared drives is EMC made, accessible through a browser, so I am assuming.
In the web.config, I have the drive names as Z:, R: etc..
I am able to mount them on the cmd line with the following line. Otherwise, there is no windows access to VFS folder through the address bar or a browser.
mount -o xx.xx.xx.xx://vfs/folder_I_need_to_Access_to. Z:
I have modified the program's code, mounted the drives and am able to access the files while debugging the code, in the web browser. However, once I publish my code to the IIS server, the code does not seem to access the files and view them, even though the drive is mounted as a network drive and accessible through windows explorer. I have checked permissions on both EMC manager, Windows and IIS.
Some of my distant colleagues have said their teams ran into such issues and fixed them, however they have no idea what to do since they do not usually have logs or bugfixing documents, so I am assuming, mine is not an isolated case.
If you need more info on the subject, feel free to ask, I have scoured the internet and applied every solution by the way. Like NekoDrive, network utility libraries, among other questions on StackOverflow.
EDIT : adding the network folders to IIS as virtual directories does not work as well, however, I am able to access the files and folders through IIS manager content view, so I assume IIS is able to scan the directories and has appropriate permissions to the folders. Also, the user running the website is a domain admin and able to access directories as well.
tldr; The program deployed on IIS cant access the network shared folders under a vfs directory, while its owner and the debugged code on localhost is able to.
I have finally solved the issue.
In case anyone else stumbles here,
Since mounting the drives was impossible, I have modified the web.config to have the IP address instead of the mounted drive's letter in IIS manager, Configuration Editor.
So instead of Z:\ , K:\ or F:\
now it says;
\\XX.XX.XX.XX\VFS\SHARED_FOLDER
and the download link is generated as follows, take note of the backslash and slashes
\\XX.XX.XX.XX\VFS\SHARED_FOLDER\file_path/file_path/file_name
Because it would clearly show the IP of the machine while the download link is hovered on, I am encrypting and decrypting the link variable during runtime.

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.

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

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.

.NET MVC Websites - shared folders between websites

I have a production website that sits on two servers that used local label files to drive their page labels (request going round robin between the two).
Users need the ability to upload new labels files, but once uploaded on one I need it also updated on the second website - this needs to be immediate. I was trying to use a shared folder on one of the servers, but even if I give it everyone full access i get the error "Exception message: Unable to find label folder at \\MACHINENAME\LabelFiles" when reading from the other server, I've also tried giving full permissions to "IIS AppPool\DefaultAppPool", but get the same issue.
I'm using IIS 7.5 on Windows Server 2008 R2
Question-
Is there a way to share a folder between the two sites?
Is there a better alternative solution?
Thanks
Both the websites should have a virtual folder pointing to the same physical folder, where the users can upload files.
Make sure also that the Anonymous access is disabled
One approach is to map the folder as a drive on each of the production machines, it should then be as simple as refereing to that particular drive letter.
This can be done by navigating to the folder in windows explorer, then clicking Map Network Drive.
I cannot guarantee this will work, buut it might be worth a go.
The IIS AppPool\DefaultAppPool user is a special account that is only local to the machine where it is being used. You cannot access network file shares with that account. You will either need to use the 'Network Service' account or a domain account as the app pool user.
Also, since you are load balancing this site between two servers, you might want to consider using some type of SAN or NAS storage that is shared between the two servers. Otherwise, you will need to come up with some kind of process to synchronize the file share on both servers.

Which permissions are necessary for a FileSystemWatcher to monitor a network share?

I have a windows service which basically consists of a FileSystemWatcher. This service works well when monitoring local folders. The service will not, however, start when I specify a mapped drive in its app.config.
I've changed the account type of the service to LocalSystem, LocalService, and NetworkService without any difference. Most of my testing has been done with LocalSystem.
Any suggestions?
You need an account that has access to the mapped drive. The ones you mentioned do not.
Make a domain account and run the service under it. Grant it local admin, if you like, but also give it full rights to the folder it needs to monitor.

Categories

Resources