stuck getting folder access permissions

  • Thread starter Thread starter G-Oker
  • Start date Start date
G

G-Oker

Guest
Hello,

I am trying to get the permissions for a specific user for a specific folder.

I have the following code, but it loops though (seemingly) every "group or user name" permissions , and list the permissions code as well as the title (ie one iteration shows up 2 lots of information (Text version of the permissions then the associated code ).

If I substitute "administrators" for the NTAccount name I am looking for it kinda works, but it still loops.

If I use a name that is NOT listed in the Windows "security" tab for the then I get multiple "user as not access to the folder" messages.


System.Security.Principal.NTAccount acc = new System.Security.Principal.NTAccount("myPCusername");
System.Security.Principal.SecurityIdentifier secId = acc.Translate(typeof(System.Security.Principal.SecurityIdentifier))
as System.Security.Principal.SecurityIdentifier;
DirectoryInfo dInfo = new DirectoryInfo(Folderlocation);
System.Security.AccessControl.DirectorySecurity dSecurity = dInfo.GetAccessControl();
System.Security.AccessControl.AuthorizationRuleCollection rules = dSecurity.GetAccessRules(
true,
true,
typeof(System.Security.Principal.SecurityIdentifier));
foreach (System.Security.AccessControl.FileSystemAccessRule ar in rules)
{
if (secId.CompareTo(ar.IdentityReference as System.Security.Principal.SecurityIdentifier) == 0)
{
MessageBox.Show(ar.FileSystemRights.ToString());
label19.Text = ar.FileSystemRights.ToString();
}
else
{
MessageBox.Show("user had no access to the folder");
label19.Text = " NONE";
}
}


What I am trying to do is to get the listed users permission (as a string, ie not the numbers) on the folder, once, so I can use it replace the text on a label (the messagebox is currently there for testing, and will be removed once it works)


I understand that the next hurdle here is to check if the users is in one of the Groups (rather than being specifically listed), so if there is a soultion that involves that also, that would be great,


Many thanks, and I hope everyone is keeping safe.

Continue reading...
 
Back
Top