Thursday 19 April 2012

Beginning .Net : Get Access Control List of File and Directory using C# Programming

There is a need to get the Access Control Lists or ACLs on directories and files.
ACLs are the way resources such as directories and files are secured in the NTFS file system,which is the file system used by most recent versions of Windows.
Manually you can view a file’s ACLs by selecting the Security tab from the file’s Properties dialog.
Here is screen shot for this.
(To view original image , click on image)

There is System.AccessControl namespace in the .NET Framework, you can query the file system for the ACL information.

Here are C# example for display ACL on web page:
        System.Text.StringBuilder objSB = new System.Text.StringBuilder();
       
        string strFilePath = "D:\\Others\\bookmarks.html";
        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(Path.GetPathRoot(strFilePath));
        Response.Write("Owner : " + sec.GetOwner(typeof(System.Security.Principal.NTAccount)).Value);

        System.Security.AccessControl.AuthorizationRuleCollection auth = sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));

        objSB.Append("<table cellspacing=0 cellpadding=0 class='tblBorder'>");
        objSB.Append("<tr><td>Identity</td><td>AccessControlType</td><td>InheritanceFlagse</td><td>IsInherited</td><td>PropagationFlags</td><td>FileSystemRights</td></tr>");

        foreach (System.Security.AccessControl.FileSystemAccessRule objR in auth)
        {
            objSB.Append("<tr>");

            objSB.Append("<td>");
            objSB.Append(objR.IdentityReference);
            objSB.Append("</td>");

            objSB.Append("<td>");
            objSB.Append(objR.AccessControlType);
            objSB.Append("</td>");

            objSB.Append("<td>");
            objSB.Append(objR.InheritanceFlags);
            objSB.Append("</td>");

            objSB.Append("<td>");
            objSB.Append(objR.IsInherited);
            objSB.Append("</td>");

            objSB.Append("<td>");
            objSB.Append(objR.PropagationFlags);
            objSB.Append("</td>");

            objSB.Append("<td>");
            objSB.Append(objR.FileSystemRights);
            objSB.Append("</td>");

            objSB.Append("</tr>");
        
        }
        objSB.Append("</table>");
        Response.Write(objSB.ToString());

Output :
(To view original image , click on image)

Here you can also pass directory path like this
strFilePath ="D:\\Others";

This will display the directory ACL information.

Here are converted code for vb.net with automated tool
Dim objSB As New System.Text.StringBuilder()

Dim strFilePath As String = "D:\Others\bookmarks.html"
Dim sec As System.Security.AccessControl.FileSecurity = System.IO.File.GetAccessControl(Path.GetPathRoot(strFilePath))
Response.Write("Owner : " & sec.GetOwner(GetType(System.Security.Principal.NTAccount)).Value)

Dim auth As System.Security.AccessControl.AuthorizationRuleCollection = sec.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))

objSB.Append("<table cellspacing=0 cellpadding=0 class='tblBorder'>")
objSB.Append("<tr><td>Identity</td><td>AccessControlType</td><td>InheritanceFlagse</td><td>IsInherited</td><td>PropagationFlags</td><td>FileSystemRights</td></tr>")

For Each objR As System.Security.AccessControl.FileSystemAccessRule In auth
    objSB.Append("<tr>")

    objSB.Append("<td>")
    objSB.Append(objR.IdentityReference)
    objSB.Append("</td>")

    objSB.Append("<td>")
    objSB.Append(objR.AccessControlType)
    objSB.Append("</td>")

    objSB.Append("<td>")
    objSB.Append(objR.InheritanceFlags)
    objSB.Append("</td>")

    objSB.Append("<td>")
    objSB.Append(objR.IsInherited)
    objSB.Append("</td>")

    objSB.Append("<td>")
    objSB.Append(objR.PropagationFlags)
    objSB.Append("</td>")

    objSB.Append("<td>")
    objSB.Append(objR.FileSystemRights)
    objSB.Append("</td>")


    objSB.Append("</tr>")
Next
objSB.Append("</table>")
Response.Write(objSB.ToString())

No comments:

Post a Comment