private void Directory_SetAccessControl(bool isNetwork) { using (var tempRoot = new TemporaryDirectory(isNetwork)) { var folder = tempRoot.CreateDirectory(); var sysIO = System.IO.Directory.GetAccessControl(folder.FullName); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount)); var alphaFS = Alphaleonis.Win32.Filesystem.Directory.GetAccessControl(folder.FullName); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("Input Directory Path: [{0}]", folder.FullName); Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count); Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); // Remove inherited properties. // Passing true for first parameter protects the new permission from inheritance, // and second parameter removes the existing inherited permissions Console.WriteLine("\n\tRemove inherited properties and persist it."); alphaFS.SetAccessRuleProtection(true, false); Alphaleonis.Win32.Filesystem.Directory.SetAccessControl(folder.FullName, alphaFS, AccessControlSections.Access); // Re-read, using instance methods. var sysIOdi = new System.IO.DirectoryInfo(folder.FullName); var alphaFSdi = new Alphaleonis.Win32.Filesystem.DirectoryInfo(folder.FullName); sysIO = sysIOdi.GetAccessControl(AccessControlSections.Access); alphaFS = alphaFSdi.GetAccessControl(AccessControlSections.Access); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); // Restore inherited properties. Console.WriteLine("\n\tRestore inherited properties and persist it."); alphaFS.SetAccessRuleProtection(false, true); Alphaleonis.Win32.Filesystem.Directory.SetAccessControl(folder.FullName, alphaFS, AccessControlSections.Access); // Re-read. sysIO = System.IO.Directory.GetAccessControl(folder.FullName, AccessControlSections.Access); alphaFS = Alphaleonis.Win32.Filesystem.Directory.GetAccessControl(folder.FullName, AccessControlSections.Access); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); } Console.WriteLine(); }
private void SetSecurityAlpha(string directory) { if (System.IO.Directory.Exists(directory)) System.IO.Directory.Delete(directory, true); System.IO.Directory.CreateDirectory(directory); System.IO.Directory.CreateDirectory(System.IO.Path.Combine(directory, "inherited")); var testDirInfo = new Alphaleonis.Win32.Filesystem.DirectoryInfo(directory); var ds = testDirInfo.GetAccessControl(System.Security.AccessControl.AccessControlSections.Access); ds.SetAccessRuleProtection(true, false); ds.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null), System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.InheritanceFlags.ContainerInherit | System.Security.AccessControl.InheritanceFlags.ObjectInherit, System.Security.AccessControl.PropagationFlags.None, System.Security.AccessControl.AccessControlType.Allow)); testDirInfo.SetAccessControl(ds); }
public static bool IsDirectoryAccessible(this DirectoryInfo directory) { try { directory.GetAccessControl(); return(true); } catch (UnauthorizedAccessException) { return(true); } catch { return(false); } }
public override DirectorySecurity GetAccessControl() { return(instance.GetAccessControl()); }
private void Directory_SetAccessControl(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var tempPath = System.IO.Path.GetTempPath(); if (isNetwork) { tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath); } using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name)) { var folder = rootDir.RandomDirectoryFullPath; System.IO.Directory.CreateDirectory(folder); var sysIO = System.IO.Directory.GetAccessControl(folder); var sysIOaccessRules = sysIO.GetAccessRules(true, true, typeof(NTAccount)); var alphaFS = Alphaleonis.Win32.Filesystem.Directory.GetAccessControl(folder); var alphaFSaccessRules = alphaFS.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("\nInput Directory Path: [{0}]", folder); Console.WriteLine("\n\tSystem.IO rules found: [{0}]\n\tAlphaFS rules found : [{1}]", sysIOaccessRules.Count, alphaFSaccessRules.Count); Assert.AreEqual(sysIOaccessRules.Count, alphaFSaccessRules.Count); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); // Remove inherited properties. // Passing true for first parameter protects the new permission from inheritance, // and second parameter removes the existing inherited permissions Console.WriteLine("\n\tRemove inherited properties and persist it."); alphaFS.SetAccessRuleProtection(true, false); Alphaleonis.Win32.Filesystem.Directory.SetAccessControl(folder, alphaFS, AccessControlSections.Access); // Re-read, using instance methods. var sysIOdi = new System.IO.DirectoryInfo(folder); var alphaFSdi = new Alphaleonis.Win32.Filesystem.DirectoryInfo(folder); sysIO = sysIOdi.GetAccessControl(AccessControlSections.Access); alphaFS = alphaFSdi.GetAccessControl(AccessControlSections.Access); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); // Restore inherited properties. Console.WriteLine("\n\tRestore inherited properties and persist it."); alphaFS.SetAccessRuleProtection(false, true); Alphaleonis.Win32.Filesystem.Directory.SetAccessControl(folder, alphaFS, AccessControlSections.Access); // Re-read. sysIO = System.IO.Directory.GetAccessControl(folder, AccessControlSections.Access); alphaFS = Alphaleonis.Win32.Filesystem.Directory.GetAccessControl(folder, AccessControlSections.Access); // Sanity check. UnitTestConstants.TestAccessRules(sysIO, alphaFS); } Console.WriteLine(); }