Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

access denied when creating a folder using ASP.NET

Posted on 2009-05-12
24
Medium Priority
?
2,356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
  • 2
24 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24363659
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
ID: 24364863
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
ID: 24364869
and by the way, that's what the account is named as... "ASPNet Machine"
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:lanier3532
ID: 24364982
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
ID: 24365033
are the computers on the same domain?
0
 

Author Comment

by:lanier3532
ID: 24365127
there is no domain, just a workgroup
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24365146
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
ID: 24365257
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
ID: 24365341
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
ID: 24365444
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
ID: 24365566
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
ID: 24365612
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
 

Author Comment

by:lanier3532
ID: 24365808
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
ID: 24365902
what's the error you're getting with this code?
0
 

Author Comment

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

Expert Comment

by:MaxOvrdrv2
ID: 24367169
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
ID: 24367709
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
ID: 24367912
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
ID: 24369846
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
ID: 24373827
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
ID: 24374451
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
ID: 24374470
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 2000 total points
ID: 24378141
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
ID: 24378589
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Recently, I read that Microsoft has analysed statistics for their security intelligence report. It revealed: still, the clear majority of windows users do their daily work as administrator. An administrative account is a burden, security-wise. My ar…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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