Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Upload images acces to path denied

Hi I seem to be having a problem when uploading images in asp.net.When I tryed to upload an Image I get this error:

Access to the path 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru' is denied.

I have set application pools Identoty to NETWORKSERVICE ando also added the NETWORK SERVICE account to the Images folder with full permision but I still get the same error.

This is my code:

private void addImageToApp()
{
    string path = "~/Images/avatar/userAvatars/" + User.Identity.Name;

    createPath(path);
    if( Directory.Exists(HostingEnvironment.MapPath(path)))
    {
        //try {
              UploadImage.SaveAs(HostingEnvironment.MapPath(path));
          //    MultiViewIndex.ActiveViewIndex = 0;
        //}catch(Exception ex)
        //{
          //     AvatarDetails.Text = ex.Message;
        //}  
    }
}

private void createPath(string path)
{  
    string activeDir = HostingEnvironment.MapPath("~/Images/avatar/userAvatars");
    if( !Directory.Exists(Server.MapPath(path)) )
    {
        string newPath = Path.Combine(activeDir, User.Identity.Name);
        Directory.CreateDirectory(newPath);
    }
}

What else can I do to solve this problem?

EDIT

Hi at this point I have full permision control to the following USERS:

Is it posible that I need to set any configuration to IIS in order for this to work?

EDIT

I have messed around with SQL-SERVER for the last couple of days in order to make this work so I might have missconfigured something form what I understand NETWORK SERVICE is stored in SQL-SERVER master.db database.I seem to be having two network service logins may this be the problem?I remember when I first checked it I had none now I have two:

enter image description here

EDIT

This is the print with the permisions I added to the folder:

enter image description here

EDIT : Complete error

enter image description here

StackTrace:

enter image description here

Upvotes: 0

Views: 3163

Answers (5)

CodeWeed
CodeWeed

Reputation: 1071

Ok... I have done this before for a project to implement a PUT method for http. I dont clearly remember it.. but some hints... if I were in my office I could tell you correctly. here are the hints

  1. You need to add IIS_IUSRS to have access to the folder in windows.
  2. Go to IIS admin console click the deployed site node, and set the permission for the same folder/website requests coming in... I dont remember the which category was it.. that settings pane will allow you to add/modify permissions for POST, GET and other verbs for that matter... when you edit that, you should see options for Administrator, a particular user account, anonymous etc.

may be I will write back tomorrow... exactly how to do it :-)

Upvotes: 1

comecme
comecme

Reputation: 6386

In method CreatePath you are creating folder 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru'.

Then, you try to save the uploaded image with the filename 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru'.

You can't have a folder and a file with the same name. If you try to do this, the OS will tell you access is denied.

What happens if you try this in a command prompt

I suppose you want to either create a filename inside folder aleczandru, or you meant to save the file as aleczandru.png or something in folder userAvatars.

Assuming your UploadImage is a FileUpload control, you can save the file to the user's folder using the original file name of the uploaded file.

UploadImage.SaveAs(HostingEnvironment.MapPath(
    Path.Combine(path, UploadImage.FileName)));

Upvotes: 5

Mert
Mert

Reputation: 6572

Pls make sure you have full filename with file extention in you path.

Upvotes: 4

Kevin
Kevin

Reputation: 704

You need to find out what user the asp.net upload page is running under. If you haven't changed it, and are not running under impersonation, it should default to the ASPNET user on the local machine. Whatever it turns out to be, give that user read/write permissions on the folder.

Upvotes: 0

kleinohad
kleinohad

Reputation: 5912

Try to give the group called users the permission to modify this directory (under security)

Upvotes: 0

Related Questions