Monday, June 22, 2009

Grant a User Read/Write Rights to Multiple Directories at Once

I recently came across the problem of having to grant the ASP.Net machine account read/write access to some 250 directories on a server without inheritance. Rather than manually update each directory, I wrote a program to automatically add the ASP.Net account with read/write rights to all sub-directories.

The Problem:

Over an 8 year period, a classic ASP website had created some 250 folders that could only be accessed by the creator and a group of administrators. The program had also turned off inheritance on the directories, so that a parent directory couldn’t add new accounts to its children. An update to the website requires that the generic ASP.Net machine account have access to read/write to the directory.

The Solution:

I created a very basic C# console application that utilizes the DirectorySecurity and AuthorizationRuleCollection classes to iterate through the subdirectories and add an account if it doesn’t have rights to that subdirectory. The code for this can be found at:

Replace “DIRECTORY_PATH” with the path to the root directory, “SERVER_NAME” with the name of your server, and “ASPNET” with the name of the account you want access granted for.