public void CreateTheFolderWithPersonnalizedNtfsPermissions_WhenSetExclusivePermissionsIsAsk() { // Arrange DirectoryInfo folderToCreate = new DirectoryInfo(@"E:\Pst Backup\Test Files\FolderToCreate"); DirectorySecurity folderSecurity; string securityDescriptor; // Act if (folderToCreate.Exists) { folderToCreate.Delete(true); folderToCreate.Refresh(); Assert.IsFalse(folderToCreate.Exists); } SUT.CreateDestinationFolder(folderToCreate.FullName, true, @"MCSA\SConnor", @"MCSA\SVaughan"); folderSecurity = new DirectorySecurity(folderToCreate.FullName, AccessControlSections.All); securityDescriptor = folderSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.All); // Assert Assert.IsTrue(folderSecurity.AreAccessRulesProtected); Assert.IsTrue(securityDescriptor.Contains("(A;OICI;FA;;;SY)")); Assert.IsTrue(securityDescriptor.Contains("(A;OICI;0x1301bf;;;S-1-5-21-2569095476-1252395535-2594552870-1105)")); Assert.IsTrue(securityDescriptor.Contains("(A;OICI;FA;;;S-1-5-21-2569095476-1252395535-2594552870-1608)")); Assert.IsTrue(securityDescriptor.Contains("(A;OICI;0x1301bf;;;S-1-5-21-2569095476-1252395535-2594552870-1609)")); }
public void ReplaceDirectory(string directory, bool recursive) { Debug.Assert(Directory.Exists(directory)); ActionObserver.NotifyAction("Search/Replace SID", "Directory", directory, string.Empty); try { try { DirectorySecurity security = Directory.GetAccessControl(directory, AccessControlSections.All); string sddl = security.GetSecurityDescriptorSddlForm(AccessControlSections.All); foreach (Tuple <SecurityIdentifier, SecurityIdentifier> item in ReplaceList) { string searchItem = item.Item1.ToString(); string replaceItem = item.Item2.ToString(); string newSddl = sddl.Replace(searchItem, replaceItem); if (newSddl != sddl) { ActionObserver.NotifyInformation( "Directory '{0}' replaced '{1}' with '{2}'", directory, searchItem, replaceItem ); } sddl = newSddl; } security.SetSecurityDescriptorSddlForm(sddl, AccessControlSections.All); Directory.SetAccessControl(directory, security); } catch (Exception error) { ActionObserver.NotifyError("Unable to search SIDs on directory '{0}' due to error '{1}'", directory, error.Message); } try { // process files foreach (string file in Directory.GetFiles(directory)) { ReplaceFile(file); } } catch (Exception error) { ActionObserver.NotifyError("Unable to search SIDs on directory '{0}' due to error '{1}'", directory, error.Message); } // process sub directories first if (recursive) { foreach (string subDirectory in Directory.GetDirectories(directory)) { ReplaceDirectory(subDirectory, recursive); } } } catch (Exception error) { ActionObserver.NotifyError("Unable to search/replace SIDs on directory '{0}' due to '{1}'", directory, error.Message); } }