Access to the path is denied - c#

I'm trying to use a ASP.NET(C#) application on a IIS-Server. Everything is working great, i have only one problem. I'm trying to create a CSV-File in the 'C:\inetpub\wwwroot\bin' directory, but i get the errormessage "Access to the path 'C:\inetpub\wwwroot\bin' is denied.".
I tried to give the 'IUSR' and the 'NT-AUTHORITY/networkservice' all rights, but it still doesn't work. I also tried it in an other directory but also doesn't work.
OS: Windows Server 2008 R2
Thanks in advance!
Edit: Thx! Works now.

use the App_Data folder for that, NOT the bin folder:
more here: http://msdn.microsoft.com/en-us/library/t990ks23.aspx
and: http://msdn.microsoft.com/en-us/library/ex526337.aspx

Try adding the rights to the ASPNET user, that might be the issue.
Also, writing a file at the root of the web-folder, is basically a bad idea.
It's much safer if you put create the files in a folder that is not accessible from the web, and than use a separate page to download the file (I'm guessing that's what you are trying to accomplish), something along these lines.

Related

ASP physical and virtual path

This is the first time I make an asp site. This line of code is working fine on my pc but obviously to make it working on the production server I need to change the reference.
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\Pink\Documents\Visual Studio 2012\Projects\ManagDoc_Framework\Test1_managDoc\Test1_managDoc\Allegati\" + recordIDcreateDir);
I have tried many sort of path combination but I am not getting it right.
I would like to find a solution that makes the code working on both pc, during development, and hosting server without having to change the code.
How should i write the path? Some help will be appreciated.
Use Server.MapPath method :
The MapPath method maps the specified relative or virtual path to the
corresponding physical directory on the server.
Additional details on W3schools.com, tutorial I followed, and where I learnt the existence of the above method.

Access to the path 'xxx' is denied (where 'xxx' is a folder)

I know this has been asked before many times, but I have browsed tens of similar questions without help.
This error is showed when my asp.net 4.0 application tries to access a folder on my local drive, doing this:
XmlTextReader confReader = new XmlTextReader (filename);
while (confReader.Read()) // <- error line
{
// do something
}
In my web.config I have <Identity Impersonate="true">. For the folder I have assigned full-access to:
everyone
IIS APPPOOL\<custom apppoolname>
NETWORK SERVICE
Debugging the application, I can put up a watch which evaluates this call right before the incriminated line:
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
the call shows the current user is IIS APPPOOL\<custom apppoolname>.
I'm on Windows 7 64bit with Sp1. I know it's probably something stupid but I have already spent an awful amount of time on this.
Edit:
The problem was in the variable "filename", which pointed to the right folder, but for a configuration problem the filename was missing (i.e. the content was "c:\data\" instead of "c:\data\file.xml"). The Asp.net error was actually confusing, since I had all the right permissions to access that folder. DJKRAZE pointed me to the right direction.
Here is what I would suggest checking
where are you declaring filename..?
Does the file even exist..?
do you have rights to that folder..? have you tried running VS as Admin..? do you have Virtual Directory setup for the web app..sounds like you have a few things configured improperly as well but can't really tell based on the code for starters this line System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
should yield your domain\\username or compuername\\username
Glad that FileName was all it was
IIS requires the path to a folder in the server machine has rights for Anonymous user login to access.
Make sure the user Anonymous has the rights to access the folder.

Writing to a file I get unauthorized access error.How can I do it?

Despite numerous post on the web I cannot find an answer to my problem.
I am writing an application that writes csv files to folders.Users should be able to pick a directory.
I am developing in windows 7 using vs2010 running my app in Admin Mode.Regardless of all this I still get the "Unauthorized access exception" when I do
var path=#"c:\" or c:\MyFolder
StringBuilder sb=new StringBuilder();
sb.AppendLine("Test");
var myFile=sb.ToString();
using (var writer=new StreamWriter(path))
{
writer.Write(myFile);
}
Am I missing something?
I have feeling that in window7 you can only write to designated folders.Is this what's happening?
any suggestions?
EDITED
I have created few folders under "C:\MyFolder\"
I am not using any credentials eg windows impersonation etc..
It does write if it writes to the bin\debug\ of my class library. but not to any designated folder.
Is your code snippet the real code causing the problem?
On the face of it, you are trying to stream the text "Test" into a directory on the file system, not trying to write a file. (path is just assigned to #"C:\"). I'm not surprised that you get an UnauthorizedAccessException.
Assign the full path of the file you want to write into your path variable, and I imagine you'll succeed.
Try running your app with "Run as Administrator". The comments above will probably also steer you in the right direction. You should definitely pick a directory that your windows users has access to edit.

Server Root and MapPath()

I have a file structure set up like this:
ServerRoot
applicationRoot
filePage.aspx
files
chart.png
My application page called filePage.aspx uses another app to custom build charts. I need them saved in files folder. This is how our client's production server is set up and I cannot change this.
I do a _page.Server.MapPath("/files") but it gives me a InvalidOperationException and states Failed to map the path '/files'.
UPDATE:
So it has to be set up this way MapPath("/"). My local asp.net server can't handle the MapPath that way, but our IIS development box has no problem with it and it works fine. Interesting.
How do I get it to save to files?
I believe it's a security violation to go outside the directory structure of the virtual directory in asp.net 2.0 and up. You'll need to make a virtual directory to the directory and use that.
Use
Server.MapPath("~/files")
The ~ represents the root of the web application so the folder returned will be correct no matter which subdirectory you are in.
Try
_page.Server.MapPath("files")
_page.Server.MapPath() will attempt to map from the root of the application (NOT the root of the server).
Try _page.Server.MapPath("../files").
EDIT
You may run into security issues when trying to map outside of your application root. If that is the case, you can do something like this:
Server.MapPath("~").Substring(0, Server.MapPath("~").IndexOf(VirtualPathUtility.ToAbsolute("~").Replace("/", "\"))) + "\files"
This looks rather complex, but essentially says "map my application, then remove the application root from the end and add '\files' instead".

Download and save aspx page

I saved aspx page as html it worked in my local machine but after published on the server its showing an error that "the access to the path is denied"... I tried giving access permission then also it doesn't work.. can anyone help with it? or else is there any other way to save the page in C# asp.net?...
string url=HttpContext.Current.Request.Url.AbsoluteUri;
string sHtml="";
HttpWebRequest request;
HttpWebResponse response=null;
Stream stream=null;
request=HttpWebRequest)WebRequest.Create(url);
response=(HttpWebResponse)request.GetResponse();
stream=response.GetResponseStream();
StreamReader sr=new StreamReader(stream,System.Text.Encoding.Default);
sHtml=sr.ReadToEnd();
string path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string textfilename=TextBox1.Text;
string getpath=path+"\\"+textfilename+".html";
File.WriteAllText(getpath,sHtml);
if(stream!=null)stream.Close();
if(response!=null)response.Close();
Thanks..
If you need to save a file use this to get the application path
Server.MapPath(#"filename.txt");
Try this because when you only write the filename at an web server you are trying to access to a Microsoft.Net folder.
yes make sure the folder you write to has the appropriate privileges. IIS 5 uses ASPNET account, IIS6 uses network service. Just give read/write to the proper account. Is this your dev environment, or a prod environment?
Also, make sure it's pointing to a folder; desktop folder for ASPNET account probably won't exist since ASPNET account isn't a user with a users folder.
I had a similar problem when trying to deploy CSS from a ZIP downloaded from the web. Try right-clicking on the aspx file from windows explorer and checking the file properties. One of the tabs may indicate that the file has been "blocked". If so, there will be a button there to unblock it.

Categories