Solved

access denied when creating a folder using ASP.NET

Posted on 2009-05-12
24
1,966 Views
Last Modified: 2013-12-04
I am trying to create a folder on server on our network from an ASP.NET script on our web server.  

The syntax is something like Directory.CreateFolder("\\Server1\BaseFolder\NewFolder").  

This syntax works fine when issued from my personal web server on my laptop via Visual Studio 2005.  When i put the aspx file on the production web server, it get "Access to Path is Denied".  I have set the security on the "BaseFolder" for Everyone to have Full Permissons.

What am i missing?
0
Comment
Question by:lanier3532
  • 12
  • 10
  • 2
24 Comments
 
LVL 12

Expert Comment

by:GuitarRich
Comment Utility
Make sure you set permissions for the following users:
Network Service
IUSR_<machinename>
Just setting the Everyone user will not always work.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
Actually, with Visual Studio 2005, (.Net 2.0), you need to set the following permissions, on the containing folder (the one you want to create your folder in):

ASP.Net Machine (account) --> Full Access

if you don't do that, it will never work.

Cheers!
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
and by the way, that's what the account is named as... "ASPNet Machine"
0
 

Author Comment

by:lanier3532
Comment Utility
I have the following accounts set to Full Control:
ASP.NET Machine Account
Everyone
Guest
Internet Guest Account

Remember, these privileges are on the target computer (a different computer than the web server that is trying to create the folder).
0
 
LVL 12

Expert Comment

by:GuitarRich
Comment Utility
are the computers on the same domain?
0
 

Author Comment

by:lanier3532
Comment Utility
there is no domain, just a workgroup
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
In that case, be sure that you have the computer mapped on the web server... the FSO uses the file system, and thus you cannot access the folders on another computer unless its mapped on the web server.

example: i can do this: Directory.CreateFolder("F:\NetFolders\Folder1")

but i CANNOT do this:

Directory.CreateFolder("\\SrvAdd1\NetFolders$\Folder1")

if you want to do the later, it might be possible, but probably requires a lot more permissions and setup on the target computer (such as permissions on a share, IIS to allow ASPNet Machine FROM servername to access... etc...)
0
 

Author Comment

by:lanier3532
Comment Utility
i created a share on the target computer with the following Full permissions:
Everyone
ASP.NET Machine Account
Guest
Internet Guest Account

when issuing the command, i get this result:
"Could not find a part of the path 'U:\testing'"

"U" is mapped to the root folder in which to create additional folders...
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
so U:\ is currently mapped on the web server, you have given it full access  ON the web server as well? (since you just mapped it, just checking to ensure that the U:\ drive has the right permissions (ASPNet Machine Account) on the web server, as well as on the target computer)...

i have the same setup here... my web server is on E: and my application folders (databases, file uploads, etc) are on D:... and i can create, modify and delete folders on D:\ without problems... e.g.: Directory.CreateFolder("U:\testing\")

However, can i see your DirectoryInfo declaration?
0
 

Author Comment

by:lanier3532
Comment Utility
the exact command being tested is:
        Directory.CreateDirectory("U:\test")
i checked the permissions on the web server's mapped drive and they are the same as on the target computer.

if i open windows explorer and type in "U:\", it opens to the proper folder and i can see folders that i manually created using the windows explorer.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
Yes but if you declare your directory info object to look at C:\ for example, it will never find the new drive since it's only looking at the C:\ drive... you need to declare your directoryinfo object (Directory), to look at U:\... here's some info on getting that done using the DirectoryInfo object:

http://www.vbdotnetheaven.com/UploadFile/munnamax/FileManager07312007122322PM/FileManager.aspx

It's in C# but you get the idea on how it works... you need to declare the directoryinfo object to tell the application which drive you're using... i'm sorry i though you'd already done that...

0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
even better, straight up from MSDN:


-=Visual Basic Copy Code=-
 
 

Imports System

Imports System.IO
 

Public Class Test

    Public Shared Sub Main()

        ' Specify the directories you want to manipulate.

        Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")

        Try

            ' Determine whether the directory exists.

            If di.Exists Then

                ' Indicate that it already exists.

                Console.WriteLine("That path exists already.")

                Return

            End If
 

            ' Try to create the directory.

            di.Create()

            Console.WriteLine("The directory was created successfully.")
 

            ' Delete the directory.

            di.Delete()

            Console.WriteLine("The directory was deleted successfully.")
 

        Catch e As Exception

            Console.WriteLine("The process failed: {0}", e.ToString())

        End Try

    End Sub

End Class

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:lanier3532
Comment Utility
i changed the code per the example above with the same result.  Here is my code:

        Dim NewFolder As String = "U:\"
        If Request.QueryString("FolderName") = "" Then
            Response.Write("**FAILED**Missing Folder**END**")
            Exit Sub
        Else
            NewFolder &= Request.QueryString("FolderName")
            Dim dirInfo As New DirectoryInfo(NewFolder)
            If Not dirInfo.Exists Then
                dirInfo.Create()
            End If
        End If
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
what's the error you're getting with this code?
0
 

Author Comment

by:lanier3532
Comment Utility
The error is:
"Could not find a part of the path 'U:\testing'"
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
Ah... ok... i find that strange... because i just copied the code myself here and it works perfectly... the only difference i used was:

Server.MapPath("U:\")

Dim dirInfo As New DirectoryInfo(Server.MapPath(NewFolder))

            If Not dirInfo.Exists Then

                dirInfo.Create()

            End If

        End If

Open in new window

0
 

Author Comment

by:lanier3532
Comment Utility
I tried your code, didn't work.
I then created a virtual directory on the web server pointing to the other server.
I used both a mapped drive (U:\) and UNC (\\Server02\) but both give me this error:

Access to the path '\\server02\test' is denied
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
well, if access is denied, it's because you haven't set the proper rights on the virtual directory rights...

its kinda strange though, because i didn't even have to do that here... i have it in front of me and it works perfectly... i have a mapped drive (E:) and i can access it from the webserver (D:) using the DirectoryInfo object without a single problem... maybe it's the security options on your network that don't allow you to access drives/folders across domains without providing the proper login/being a registered user even if you added the ASPNet machine to the group/gave it the proper rights? this is getting pretty complicated for something i can do here without problems... and we have a pretty strict network policy too so i don't see why this wouldn't work...

what framework are you on?
0
 

Author Comment

by:lanier3532
Comment Utility
we are using 2.0.5 .net framework
i just created an entirely new folder gave permissions to these accounts again on the file server.
i then created a new virtual directory on the web server pointing to the new location.  From the web server, i can create folders and copy files onto the file server just fine using windows explorer.
it's only when running the .aspx code that it fails.  
i get the same "Access to the path '\\server02\basefolder\test' is denied" error message.
i also gave the web server full permissions to Everyone thinking the .aspx script perhaps didn't have permission to write to the file server....
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
ok... well in that case, here's how it should be for your situation, and this is the same setup i'm using (in a WIN2K env.):

Direct Mapping

web server:
- mapping to file server under, for this example, drive E: (just a normal mapping, reconnect every boot type deal)
- right-click on E: in windows explorer, click properties, then security, give full access to the ASPNet Machine Account
- in your code, remove the server.mappath, just call it directly/create the directory directly by calling the drive name.
file server:
- access the folder where your share is, right click, properties, security, find the <machinename> corresponding to the web server and give it full access (this probably already done in your case, or else you wouldn`t be able to map to it, but make sure its there)
- i`m not sure about this one, but if you can find a ASPNet_<machinename>(webserver) account, also give it full control.
- give Everyone, full control to the share as well.
 
Web Mapping (virtual directory)

The setup is exactly the same as above, except of course for the code part (you will use server.mappath), and the following:
- Go into IIS, create your virtual directory, and give the ASPNet machine account full control over it.

That, technically, should work... at the same time as you are trying this, i will confirm with a friend of mine who`s a network admin here and make sure it`s all good. Cool?
Cheers!
0
 

Author Comment

by:lanier3532
Comment Utility
how do i reference the machine name of the Web server from the File Server?  If I click on Add, the "Look in" box is greyed out.  Is this because we are not in a domain, but in a workgroup?
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
could be... but usually it's greyed out and there's an advanced button which you use to add the account.
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
Comment Utility
OK! good and bad news...

bad news:

the way they have it setup here is actually done by the admin and i don't see it myself so i didn't know what he had done so the solution given originally is wrong...

good news:

i now know how to do it... you must impersonation to do it... you see, ASPNet account is local only... therefore, you must setup your machine config on the web server to impersonate a common user to both boxes, which has the proper rights for access to the folder.

Create a user... let's say "foldercreator" on BOTH machines... give it access rights to the mapped drive on the webserver and directly on the folder on the files server... then fix your machine config on the webserver to impersonate itself as "foldercreator" whenever it tries to connect to the mapped drive.

In your code-behind, the code will remain the same (without server.mappath), and it should work perfectly...

that's why i thought that all you had to do was map the drive since that's how it looks to me when i check it... but the server admin here actually setup the machine config for it without telling me... sorry about that... hope this helps.

Cheers!
0
 

Author Comment

by:lanier3532
Comment Utility
i can't tell you how much i appreciate your help.  You've certainly spent a lot of time on this and given timely responses.  However, i think i have solved this in a different way.  I enabled web services on our file server and put the web page there that we have been working with.  THen i created a new page on the original web server which simply does a redirect to the file server!  i hate the kludge but it seems to work fine and performance is OK.  I'll award you the points for your effort and, again, i really appreciate your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
No security measures warrant 100% as a "silver bullet". The truth is we also cannot assume anything but a defensive and vigilance posture. Adopt no trust by default and reveal in assumption. Only assume anonymity or invisibility in the reverse. Safe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now