public FileSystemAccessRule GetFileSystemAccessRule <TSecurity>(
     SecurityTemplate template,
     WindowsUser windowsUser)
     where TSecurity : FileSystemSecurity
 {
     if (typeof(TSecurity) == typeof(DirectorySecurity))
     {
         // Directory security with default inheritance (files and subfolders).
         return(new FileSystemAccessRule(
                    windowsUser.Sid,
                    template.GetFileSystemRights(),
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    template.GetAccessControlType()));
     }
     else if (typeof(TSecurity) == typeof(FileSecurity))
     {
         // Files must not have inheritance specified.
         return(new FileSystemAccessRule(
                    windowsUser.Sid,
                    template.GetFileSystemRights(),
                    template.GetAccessControlType()));
     }
     else
     {
         throw new NotSupportedException(
                   "File system security type '" + typeof(TSecurity).FullName + "' is not supported.");
     }
 }
 protected void RemoveMatchingRules <TSecurity>(
     TSecurity security,
     SecurityIdentifier sid,
     SecurityTemplate template)
     where TSecurity : FileSystemSecurity
 {
     RemoveMatchingRules <TSecurity>(security, sid, template.GetAccessControlType());
 }
        private void deleteFromFileSystem(SecurityTemplate securityTemplate, SecurityIdentifier sid)
        {
            string        fullPath      = securityTemplate.GetFullPath(ServerConfig.WebsiteDirectory);
            FileInfo      fileInfo      = new FileInfo(fullPath);
            DirectoryInfo directoryInfo = new DirectoryInfo(fullPath);

            // If it exists, delete it, if not, ignore the delete request.
            if (fileInfo.Exists)
            {
                FileSecurity security = File.GetAccessControl(fileInfo.FullName);
                RemoveMatchingRules(security, sid, securityTemplate.GetAccessControlType());
                File.SetAccessControl(directoryInfo.FullName, security);
            }
            else if (directoryInfo.Exists)
            {
                DirectorySecurity security = Directory.GetAccessControl(directoryInfo.FullName);
                RemoveMatchingRules(security, sid, securityTemplate.GetAccessControlType());
                Directory.SetAccessControl(fileInfo.FullName, security);
            }
        }
        private void applySecurityAccessRule <TSecurity>(
            SecurityTemplate securityTemplate,
            WindowsUser windowsUser,
            TSecurity security)
            where TSecurity : FileSystemSecurity
        {
            RemoveMatchingRules <TSecurity>(
                security,
                windowsUser.Sid,
                securityTemplate.GetAccessControlType());

            // Add new rule (effectively replace if removed) to apply security.
            security.AddAccessRule(GetFileSystemAccessRule <TSecurity>(securityTemplate, windowsUser));
        }