0

The following code does not work in .net 6 class library, because File.GetAccessControl method is not available in .net 6.

I need a function that will retrieve all the domain user groups that have read access to a file.

public List<string> GetFileReadGroups(string filePath)
        {
            var fileSecurity = File.GetAccessControl(filePath);
            var readRules = fileSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier));
            var groups = new List<string>();
            foreach (FileSystemAccessRule rule in readRules)
            {
                if ((rule.FileSystemRights & FileSystemRights.ReadData) == FileSystemRights.ReadData)
                {
                    if (rule.IdentityReference is SecurityIdentifier sid)
                    {
                        var account = (NTAccount)sid.Translate(typeof(NTAccount));
                        groups.Add(account.Value);
                    }
                }
            }
            return groups;
        }
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.