How to change folder permissions in a web settings project?

I am using a web installation project to install my ASP.NET application that needs to be written to a folder that exists in the folder of the main virtual directory. How to configure the installation project to grant ASPNET access to this folder to users?

+4
source share
1 answer

The way to do this is to create a class derived from System.Configuration.Install.Installer . Override the Install() method. Below is an example that changes permissions for a directory and a file, you probably do not want to be so permissive, but it depends on your security context. For this to work, the installation project must run this as a custom action. Add the β€œPrimary Output” from any project in which this class is located. You also need to pass the directory to a custom action in its properties. The first variable name must match the code. For example: /targetdir="[TARGETDIR]\"

 [RunInstaller(true)] public partial class SetPermissions : Installer { private const string STR_targetdir = "targetdir"; private const string STR_aspnetUser = "ASPNET"; public SetPermissions() { InitializeComponent(); } public override void Install(IDictionary stateSaver) { base.Install(stateSaver); Context.LogMessage( Context.Parameters .Cast<DictionaryEntry>() .Select(entry => String.Format("String = {0} Value = {1}", entry.Key, entry.Value)) .Aggregate(new StringBuilder("From install\n"), (accumulator, next) => accumulator.AppendLine(next)) .ToString() ); string targetDir = Context.Parameters[STR_targetdir]; string dbDir = Path.Combine(targetDir, "db"); AddFullControlPermissionToDir(dbDir, STR_aspnetUser); string rimdbSqliteFilename = Path.Combine(dbDir, "db.sqlite"); AddFullControlPermissionToFile(rimdbSqliteFilename, STR_aspnetUser); string logsDir = Path.Combine(targetDir, "logs"); AddFullControlPermissionToDir(logsDir, STR_aspnetUser); } private static void AddFullControlPermissionToDir(string dir, string user) { DirectorySecurity directorySecurity = Directory.GetAccessControl(dir); directorySecurity.AddAccessRule( new FileSystemAccessRule( user, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); Directory.SetAccessControl(dir, directorySecurity); } private static void AddFullControlPermissionToFile(string filename, string user) { FileSecurity fileSecurity = File.GetAccessControl(filename); fileSecurity.AddAccessRule( new FileSystemAccessRule( user, FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(filename, fileSecurity); } } 
+6
source

All Articles