• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 33
  • Last Modified:

C# Retrieve just the user accounts when displaying folder permissions and exclude system accounts

I have a windows desktop application that gets the file access rules for a particular folder and displays them in a list box. The code is below. It displays everything, including system accounts (NTAuthority\System, NTAuthority\Local Server) with the individual user accounts (abc\mike.smith, abc\jan.jones). I'd like to filter out the system accounts and display only the user accounts. Is there a way to do this? I'm using C#, but this is my attempt at working with Active Directory. Thanks for any input.

private void GetFolderPerms()
{
Directory.Security dSecurity = Diectory.getAccessControl(@"C:\MyFolder');
foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
     {
        lstPerms.Items.Add(rule.IdentityReference.value);
      }
}

Open in new window

0
dodgerfan
Asked:
dodgerfan
  • 2
1 Solution
 
Chinmay PatelEnterprise ArchitectCommented:
Hi Dodgerfan,

The code you have provided is not right. I am not sure it is deliberate or someone is messing with you (if someone gave you that code - that is).

Anyways, to answer your query
Replace
{
        lstPerms.Items.Add(rule.IdentityReference.value);
      }

Open in new window

with
string ruleValue = rule.IdentityReference.Value;

                if (!ruleValue.StartsWith("BUILTIN\\") && !ruleValue.StartsWith("NT AUTHORITY\\"))
                {
                    lstPerms.Items.Add(rule.IdentityReference.Value);
                }

Open in new window


Regards,
Chinmay.
0
 
dodgerfanAuthor Commented:
Perfect, thank you!
0
 
Chinmay PatelEnterprise ArchitectCommented:
Glad I could help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now