0

trying to upload a file to an already created folder but this error keeps coming

Access to the path 'C:\Users\Joe\Documents\Visual Studio 2015\Projects\training\Site1\Site1\Content\ProductImages' is denied.

and then below is this

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\Users\Joe\Documents\Visual Studio 2015\Projects\training\Site1\Site1\Content\ProductImages' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:

Line 73:                             var path = Server.MapPath("~/Content/ProductImages");  // guardo en la variable path la direccion donde quiero guardar las imagenes, este path es un string que tu lo conformas segun el interes, puedeser upload/nombre_fichero/fecha/etc
Line 74:                             string pathdir = Path.Combine(path, imageName);
*Line 75:                             file.SaveAs(path); // store file*
Line 76:                             var imagen = new Image();
Line 77:                             imagen.ImagePath = pathdir;

and line 75 is in red When checking the granted rigths there is no restriction for storing or in the file to be stored

shark6552
  • 101
  • 1
  • 2
  • 12
  • This is tiring me , cannot find the solution ,. I checked the remaining code in that method after commenting out line 75 and is working fine (tables has the record for saved path ) but it is the saving of the actual file(image) that is causing problem with the OS. I already shared with everyone in properties, I did what the error is asking but still access is denied. – shark6552 Aug 24 '16 at 05:47
  • Did you specifically give FULL CONTROL to EVERYONE in the folder properties? Sharing won't work. – Shannon Holsinger Aug 28 '16 at 16:45
  • This all I have tried and non has solved the problem. I set in properties>Security>advanced changed the owner to **network service ** since this is what the error is telling me to do and NS is the identity in IIS7 – shark6552 Sep 01 '16 at 05:50
  • Then I added everyone,IUSR, my machine name\IIS_IUSRS and in all cases I granted full control... NO SOLUTION – shark6552 Sep 01 '16 at 05:53
  • I created a new folder where to store the uploaded images, changed in the code the path to "~/NewFolder" but still file.SaveAS(path) is denied. – shark6552 Sep 01 '16 at 05:54
  • the only thing that I have not done is try this in another pc .Is there any other way to save files(any type) in an specific folder that don us file.SaveAs(string)? any lead? Thanks. – shark6552 Sep 01 '16 at 05:55
  • OK we need to do a test. Please see my revised answer below. – Shannon Holsinger Sep 01 '16 at 11:42
  • If I could see you in real time, I could help you figure this out in a few seconds probably. I don't know how to send a chat message... I could send you a GoToAssist request and conference to your computer. I'm trying to figure out how to send you a private message to get this going. – Shannon Holsinger Sep 02 '16 at 22:37

7 Answers7

1

The security is coming from the OS, not the compiler. Navigate to your folder in Windows, right-click, change security.

If deploying, be sure to use a folder that is automatically granted write permissions to .NET - like the User/Appdata/Roaming folder.

EDIT: You are saying you've added EVERYONE to the folder and given them full control but are still getting no joy. Please try this test:

private static void writeText()
    {
        using (TextWriter tw = new StreamWriter(@"C:myTestFile.txt"))
        {
            tw.WriteLine("Hello World",false);
            tw.Close();
        }
    }

Run the above from your solution, then check you DEBUG folder for your solution (I've forgotten at this point if we're dealing with console or form or web, so you may have to search your computer for the mytestFile.txt file after running).

The point in running this is that C: (with no backslash) points to your project folder and will automatically have adequate permissions for write. If this works and you get a mytestFile.txt after running, then we go in one direction. If you get an access error, we go in another direction. So please run in your solution or a new console app (your solution would be best) and report back with the results. Have patience buddy - we'll do the best we can to get you where you need to be!

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
  • Not Working, I even added Network service as ASP.NEt base identity( as the error desciption states) but nothing – shark6552 Aug 24 '16 at 02:28
  • Thanks @Nick - you have to add Everyone to the list and give them full control. See if that works first - then you know it's an OS security issue. – Shannon Holsinger Aug 27 '16 at 23:10
  • Please see the text below EDIT: above and report back with yyour findings – Shannon Holsinger Sep 01 '16 at 11:48
  • Hey Shannon sorry for my ignorance. where will this method should be Inside the same controller thats is causing problem? somewhere else? – shark6552 Sep 02 '16 at 02:18
  • Shannon ca you read my comments below , to an answer given by Muj? What do you think is happenning? – shark6552 Sep 02 '16 at 02:33
  • put this method in the same class you call the save from (you might want to take out STATIC if your class is an instance). Call it right above where you try to cave your file, like line 21: writeText(); line 22: file.saveAs() run. It will run this method BEFORE trying to save your file. Report back with the results! – Shannon Holsinger Sep 02 '16 at 21:59
  • Step 2 (if this works and you see the text file in your project folder) is to try to save your file using the path C: (no backslash). If you can do that, we know it's a permissions thing. – Shannon Holsinger Sep 02 '16 at 22:00
0

When you host an application in IIS, IIS will run the application under another user name depending on your application pool settings. It is a good idea to know the Application Pool (What is an IIS application pool?), but in your specific case, you do not have to go through the learning curving.

The easiest way is to figure out the actual user name IIS is using by printing out the System.Environment.UserName and grant this user to the directory you want it to access.

Community
  • 1
  • 1
acai
  • 459
  • 4
  • 9
0

The file permission error is coming from your Windows OS Explorer.

It gives you the exact fix:

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Give this a shot.

hmiedema9
  • 948
  • 4
  • 17
  • according to the error the identity of the account is Network Service. I gave full control and also set it as owner but didnt work – shark6552 Sep 01 '16 at 05:59
0

Try to check the Sharing type of the folder.

Right click->Properties->Sharing Tab->Click Share... Buttong->Then Choose Everyone then add Then change it to read and write.

Then test your program again.

if error still exist try to add the folder to your solution explorer.

Click Solution Explorer->Show All files->Then Find the folder where it was then Include in the project.

Just give it a try.

Muj
  • 146
  • 2
  • 21
  • After trying a million of stuff your comment pointed me to something that I haven tryed. – shark6552 Sep 02 '16 at 02:20
  • The way the code is now on line 75 keeps giving an error BUT if I change ( and I did it only out of desperation trying random things) line 75 to make .SaveAs receive as argument pathdir, the combination of path and file name(pathdir is now the complete path to the file I want to save), the app DOES NOT throw an error and works almost fine – shark6552 Sep 02 '16 at 02:22
  • except that still doe not "SAVE" the file. I use the "" because I did what you just said Show All Files and the file is there now just that is not included in the solution...what is this? I dont know why? any idea? – shark6552 Sep 02 '16 at 02:29
  • So in short file.SaveAs(path) this goes no where. file.SaveAs(pathdir) it save the files under the ProductImages folder but is not included in the solution so the solution can not use it – shark6552 Sep 02 '16 at 02:31
0

We have to give write permissions to "IIS_Users" for folder "ProductImages" which are trying to upload, since IIS used this user to access your upload folder. Please see here how to add IIS_Users to your folder. Hope this helps you.

0

Check the permission of that folder, right click on the folder, remove selection from the check box next to "Read Only", then click on "Security" => "Edit" => Select "Users" and tick the check box for "Full Control" and "Apply"

JPil
  • 191
  • 2
  • 15
0

Having the path with just folder name and without file name results in Access Denied error. Check whether the filename is not empty/null.