asp mvc textreader - asp.net

I am trying to use a textreader to retrieve data from a text file on a local machine. I have an upload routine to get the file for the reader to open.
When I run the application locally, it works fine. When I publish my application to my server, I am getting a Could not find a part of the path error. I assume this error has to do with permission in that the server cannot read from the local file system, but I don't know the proper way to remedy this.
do I set up the application to impersonate the user? I know I can't possibly set up each local users machine to allow the iusr account to access it.
Thanks for any thoughts.
UPDATE ****
I ensured that network service is they user for the app pool. I also ensured that the networkService has access to the local folder that the application resides in. I am running IIS7, and when I test the site, I am getting the error -
The server is configured to use pass-through authentication with a built-in account to access the specified physical path. However, IIS Manager cannot verify whether the built-in account has access. Make sure that the application pool identity has Read access to the physical path. If this server is joined to a domain, and the application pool identity is NetworkService or LocalSystem, verify that <domain>\<computer_name>$ has Read access to the physical path. Then test these settings again
I have tried suggestions from various posts, including adding domain\servername$ to the folder to allow access, but nothing seems to work.
Any thoughts?

Assuming this is IIS 6.5 or higher, the application pool user must have the necessary permission. By default, this is Network Service, which likely does not.

I solved this issue by first saving the file to the server, and then accessing the server's local file -
savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(file.FileName));
file.SaveAs(savedFileName);
The following blog helped me out - http://www.hanselman.com/blog/default.aspx?date=2008-06-28

Related

IIS7 Accessing Network Share

I am running IIS 7 on Windows Server 2008 R2 with PHP 5.4. One of my PHP scripts is trying to access a file on a protected network share using a UNC path. How can I change the IIS service account to an account that has permission to access the share? This is really easy to do on Apache HTTP server (you just change it), but it's not clear how to do with IIS. What can I do?
Update:
I was able to get things working using the "Connect As" option in the "Basic Settings" of my website and then specifying an account that has access to the network share. It appears that my problem is related to this question:
https://serverfault.com/questions/366234/iis-identities-application-pool-vs-connect-as-in-basic-settings
For IIS 7 running on Windows Server 2008 R2 ... In the IIS Manager, select the Application Pool under which your Web Site is running.
Click "Advanced Settings". There will be an entry for Identity (it is under the Process Model section). Click it, provide credentials for your account that has permission to access the share.
UPDATE
You should make sure that if you are using an Active Directory Domain Account, you provided that correctly under Identity for the running App Pool. For example, MYDOMAIN\myAccount.
After making this change, you will need to do the following:
Stop the Web Site.
Recycle your Application Pool.
Start the Web Site.
UPDATE II
From the comment discussion on this answer, #HydroPowerDeveloper was able to get the PHP script to be able to access the network share via UNC path using WebSite -> Basic Settings -> "Connect As" and setting the credentials there.
In the past, I have always used the approach of setting Identity via Application Pool and that has allowed my code to access Network shares via UNC path.
However, all of the sites/applications I have deployed on IIS are .NET based WCF or ASPX sites.
I would speculate (but am not 100% certain on this, would need research/testing to confirm) that the Identity specified in the Application Pool is used by executing .NET code, whereas the "Connect As" is used by the PHP script.
Generally the Application Pool is set using the least amount of privileges as possible.
http://msdn.microsoft.com/en-us/library/ff647402.aspx
The easiest route is to allow access to the current IIS account.
In Explorer, navigate to the shared directory.
Right Click -> Properties -> Security
If you don't see the expected IIS user in the list (Group or user names), edit and add the user as required.
I used "NETWORK SERVICE" -> Check names.
The detailed instructions are on that site.
In this way, the system is still restricted, we're ONLY allowing access to specific directories, we don't need to create any new users, we don't need to recycle the application pool and we don't need to perform any nifty impersonation code.
To resurrect an old question...
I've just been setting up a Win 2016 server that uses some Perl to access a UNC share. I set the Application Pool to an account that I verified had the correct permissions on that share. I then set up my site to 'Connect as...' (under 'Basic Settings') the same user. I still couldn't get access working.
Much head scratching later, I realised that my 'Virtual Directory', cgi-bin, was running under the original 'pass through' account. I then set up the virtual directory to 'Connect as...' the same account as the site and the application pool and, bingo, it all works fine.
So, remember to update any Virtual Directories as well as the site and App Pool...

IIS - Required permissions cannot be acquired. Enabled "Full Trust" and Load User Profile on App Pool is "True"

Setup
There is an IIS 6 application running in a .NET 2.0 App Pool on Server-A. IIS 7 Server-B needs to host that same code-base under a different URL. I created a virtual directory under Server-B's website to the network path of the application on Server-A. I've enabled this virtual directory on Server-B as an application running in its own .NET 2.0 App Pool in Classic Mode. Both app pools run as the same domain account that has access to that folder.
Issue
When accessing the site from Server-A it works fine. Accessing it from Server-B however generates the infamous "Required permissions cannot be acquired." error.
I have been trying to find a solution to this problem for two days. The two most common solutions have been to check the CLR Trust level on the Server-B box or to set the Load User Profile property on the App Pool to "True". Since I have full administrative rights I set the Trust level to Full and set the profile sitting to "True". Strongly naming all the assemblies the application and throwing them in the GAC is not where I want to go.
I'm sure I'm just missing some configuration somewhere. Any ideas? Thanks.
We have had a similar problem once.
In our case the user that was the identity of the app pool have never logged on to the server. Therefore, there was no local user profile, therefore there was a problem when the system tried to use the profile.
As a test try and set the identity of the app pool to the admin account you use to log in with.
Manually verify that user can access that share. Try logging into windows as that user and then accessing that share (or fake it with something like a "runas /User:{Domain\UserName} net use {\server\share}").
Also try loading ShareMonitor wherever the files actually live. It should tell you what user account is trying to access a given share. It might not be the account you think it is. I cannot recall if it was this program or not, but I have used something very similar in the past to figure out a really odd permission problem.
That should determine if it is a file access problem. If it isn't, then all I can say is to make sure you have ASP.NET enabled (on windows server it is a separate option).
Also check out the file permissions on your "Temporary ASP.NET Files" directory. I've had to explicitly add users write access to that before to get things working.
in my case my IIS App Pool had LoadUserProfile=false, so it was not loading the profile of the app pool's Identify (a domain account). This caused this unable to acquire permissions even though my Webroot files/folders (including /bin) had correct permissions. Setting LoadUserProfile=true fixed the issue for my setup.
I would set that application pools identity attribute (in advanced settings) as administrator. That way, you application will work on the server, just like it works on the local setup.

ASP.NET access files on another computer shared folder

I have ASP.NET project which do some file access and manipulation, the methods which I use for file access are below. Now I need to access files on another server shared folder, how to do that? I easily can change file path to shared folder path but I get "can't access" error because shares are password protected.
As I understand I need somehow to send credentials to remote server before executing methods below. How to do that?
FileStream("c:\MyProj\file.doc", FileMode.OpenOrCreate, FileAccess.Write)
Context.Response.TransmitFile("c:\MyProj\file.doc");
Regards,
Tomas
An ASP.NET application (by default) will execute in IIS6 under the "ASPNET" computer account. You therefore have a couple of options:
Configure your ASPNET application to run under a (weak) domain account with permissions to access the remote computer's share
Set the permissions on the share to enable access to "Everybody" (not recommended)
Disable Forms authentication and use Windows authentication in your ASP.NET app. Turn off impersonation in web.config and IIS should pass the credentials of the user who is currently using your web application through to the underlying share (I think).
The latter option is only useful, of course, if your users all have domain accounts on your intranet, for instance. I'll continue to look around for ways to add credentials but I'm not sure off the top of my head if that's possible.
HTH,
Richard.

ASP.NET + Access to the path is denied

I have an ASP.NET application that attempts to write files on the web server during runtime. In my development environment, this works. However, when I deploy it to the production server, and I execute the code, I receive an error that states:
"Access to the path 'C:\Inetpub\wwwroot\MyWebApp\DirToWriteTo\' is denied."
My production server is a Window Server 2003 machine. The web application is set to allow anonymous access via the IUSR_TEMPLATE account.
What am I doing wrong?
Thank you!
You need provide the grant permissions to NETWORK SERVICE user.
you can check this link:-
http://repeatgeek.com/technical/asp-net-access-to-path-is-denied/
The most likely problem is that your anonymous user does not have write access to the location.
What I would do is create a web.config entry for the location you want to write to and use that config key as your writable directory. This will allow you to have separate configuration for your production server and your development machine, if needed.
Next, just give write access to the directory defined in your configuration to the user IUSER_TEMPLATE, or as Thorarin said the Network Service, in which case you should set up identity impersonation in your Web.config so that you can specify the user (unless you configure the user through the app pool). That should fix the problem.
If you have Windows 2008 or above, try giving write permissions to IIS_IUSRS.
(Network Service has also worked for me in the past. From what I read here, it depends on the Server OS.)
In my case, Visual Studio 2015 changed an existing project's IIS AppPool user to the default of DefaultAppPool, which gave me permission errors, configuration errors, role provider errors, and null reference errors. After discovering this and changing it back to the application pool that the folder permissions were set to, things started working again.

IIS 6.0 Access Permissions to Remote Share

When setting up a virtual directory under IIS using a UNC path to a remote share, what user identity should I be expecting to see the read request to the share come under?
I've setup the above scenario and configured the user under 'Connect As' as a known user who has permissions on the remote share, which I checked with Windows Explorer.
However, when trying to access a file from within a web browser on the share using http://localhost/myfiles/atestfile.txt, I am getting an HTTP Error 401.5 - Unauthorized: Authorization failed by an ISAPI/CGI application error returned. 'myfiles' is mapped to the remote share and atestfile.txt resides in the shared folder.
If I use sysinternals filemon application to see what process and user the read request to the share is being carried out under, then I get the details that the request is through the w3wp.exe process and the user is shown as "NT AUTHORITY\NETWORK".
I was expecting the user to be shown as the user I confugured under the 'Connect As' option.
Any guidance appreciated.
If i recall correctly (used iis about 4 years ago) it was IUSR_. I'm not sure that it's still so, but give it a try.
Edit: try to give a look at this post, maybe you will find some clues ;)
Did you try it also with other file extensions than "txt", maybe "test.html"? What DLL is used for txt in the ISAPI extensions configuration (web -> Home Directory -> Configuration...)?
The section "To configure security and authentication for a virtual directory" in the MSDN article "How to: Create and Configure Virtual Directories in IIS 5.0 and 6.0" should contain the information you'll need for finding the right configuration.
I guess the account used in the security settings should be able to access the network share. This could be accomplished inserting the account in the right group or changing that account.

Resources