public static bool UserHasRights(FileSystemSecurity sec) { NTAccount usersAccount = GetUsersAccount(); return Enumerable.Any(Enumerable.OfType<FileSystemAccessRule>(sec.GetAccessRules(true, true, typeof(NTAccount))), r => { if (r.FileSystemRights == FileSystemRights.FullControl && r.AccessControlType == AccessControlType.Allow && r.InheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit)) return r.IdentityReference == usersAccount; return false; }); }
internal static bool UserHasRights(FileSystemSecurity sec) { NTAccount usersAccount = GetUsersAccount(); return sec.GetAccessRules(true, true, typeof(NTAccount)).OfType<FileSystemAccessRule>().Any<FileSystemAccessRule>(r => ((((r.FileSystemRights == FileSystemRights.FullControl) && (r.AccessControlType == AccessControlType.Allow)) && (r.InheritanceFlags == (InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit))) && (r.IdentityReference == usersAccount))); }
protected IList<FileSystemAccessRule> FindAccessRules(FileSystemSecurity security) { System.Security.Principal.NTAccount typedAccount = GetNTAccount(); List<FileSystemAccessRule> result = new List<FileSystemAccessRule>(); foreach (FileSystemAccessRule fileSystemAccessRule in security.GetAccessRules(true, false, typeof(NTAccount))) { if (fileSystemAccessRule.IdentityReference.Value.Equals(typedAccount.Value, StringComparison.InvariantCultureIgnoreCase)) { result.Add(fileSystemAccessRule); } } return result; }
private void GetAccessRules(FileSystemSecurity fsSecurity, PropertyInfo file) { try { var getOwner = fsSecurity.GetOwner(typeof (SecurityIdentifier)); if (getOwner != null) { string ownerIdentifier = fsSecurity.GetOwner(typeof(SecurityIdentifier)).Value; //var owner = fsSecurity.GetOwner(typeof(SecurityIdentifier)); //var nameOwner = owner.Translate(typeof(NTAccount)).Value; file.Owner = ownerIdentifier; } } catch(Exception ex){ /* System.Security.Principal.IdentityNotMappedException was unhandled Message="Some or all identity references could not be translated." */ SaveExc.Save(ex); } WindowsIdentity wi = WindowsIdentity.GetCurrent(); AuthorizationRuleCollection rules = fsSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier)); foreach (FileSystemAccessRule rl in rules) { GetAccessRules(wi, rl, FileSystemRights.FullControl, file); GetAccessRules(wi, rl, FileSystemRights.Modify, file); GetAccessRules(wi, rl, FileSystemRights.Read, file); GetAccessRules(wi, rl, FileSystemRights.ReadAndExecute, file); GetAccessRules(wi, rl, FileSystemRights.Write, file); } }