ACL in ASP.NET
Suppose you need to set programmatically the ACL of a file in a .NET application. For example, ensure that a certain XML file deployed with your new ASP.NET application allows the ASPNET user (or NetworkService or any other particular user) to edit it. In most cases, the administrator will be more than happy (so to speak...) to take care of it and change the ACL for you.
However, should you have the need to accomplish that task programmatically, be ready to face a bad and a good news. The bad news is that you must necessarily resort to Win32 API calls and in particular to the SetNamedSecurityInfo API function from advapi32.dll. Neither version 1.0 of the Framework, nor 1.1, and probably not even Whidbey, will provide a redesigned managed API for system security.
In the end, either you write a managed wrapper for the API (sample code is available at http://www.gotdotnet.com/team/csharp/code/default.aspx) or resort to an extremely handy tool that ships with the operating system: cacls.exe.
cacls.exe [resource] /E /G: [user_account]:F
The /E switch indicates that you want to edit the security descriptor, not replace it. The /G switch indicates that you want to add a new user to the group with the specified privileges. If the specified user exists in the group, the existing account is modified. The F argument means that you want to give the account full control over the specified resource.
This is exactly what ASP.NET applications need for a smooth setup if they need to deploy writable files. The best way to integrate this code with the setup is by defining a custom action on the Visual Studio .NET setup project and use that command line for it.
PS1: I was told that the setup of ASP.NET itself uses this trick
PS2: cacls.exe is pronounced "cackles"
Cackles is vaguely similar to an Italian word that indicates the delicate art of keeping the nose clean. What do security API and snot have in common? When you're done with both, you definitely feel better :-)