Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Set write permission on folder create

Hi I have been trying to properly configure IIS 6 to give write permisiosn for about 2 days now and I can't seem to find any good resource on this.I am a bit new to ASP.NET and until now I never had to work with IIS.

What I am trying to do is upload a file to the server.Each user on the server will have his own special folder witch will be created automaticly via C#.Now when I try to upload the file I get this error:

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

This is my code for creating the folder for each user and saving the file:

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);
    }
}

All I could find on the internet is that I have to add write permision via folder/properties/security/... while that is all good and fine I can not do this for each folder.

Up to this point I am not really sure that IIS is the one I need to configure I am a bit lost at this.

What do I have to do to give folders write permisions automaticly on folder create?

And if anyone has a good article or tutorial that shows how to do this please share it with me all the info I could find were very basic.

EDIT

I have added the network service account to the Images folder with full permision and have set the application's pool Identity to NetworkService but I still get the same error

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

Upvotes: 1

Views: 3789

Answers (2)

raheel khokhar
raheel khokhar

Reputation: 366

This has nothing to do with IIS, brother. All you need to do is, open your IIS. Go to Application Pool. Right click on the Application Pool your website is running with. Select "Advanced Settings" and see the entry made against IDENTITY. Change it to use NetworkService. This will mean that you will be running your website under NETWORK SERVICE account now on.

Now right-click your root images folder, i.e., "Images". Select PROPERTIES. Select SECURITY. Add user NETWORK SERVICE, and give it FULL RIGHTS permissions on the folder.

Now whenever you create a folder and file under this "Images" folder through your code, it will automatically inherit permissions from its parent and you will allow you to do whatever you want (Add/Delete).

I hope this answers your question. If yes, then please mark it as "answered".

Upvotes: 0

martavoi
martavoi

Reputation: 7092

Simply, when your asp.net application running you can open Task Manager and find process w3wp. W3wp process like any other has user identity (by default - application pool identity - DefaultAppPool(like application pool name)). And if it so, you should add write permissions for user named DefaultAppPool. To do it you should open security tab in folder's properties window, then change->add and type IIS AppPool/DefaultAppPool and choose local machine.

This post should help you!

Upvotes: 2

Related Questions