ASP.NET Tutorial/File Directory/Access Control

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Access Control List information (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Security.AccessControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
       System.Security.AccessControl.FileSecurity sec = File.GetAccessControl(Server.MapPath("Data.txt"));
       
       this.Label1.Text = sec.GetOwner( typeof(System.Security.Principal.NTAccount) ).Value;
     
       AuthorizationRuleCollection auth = sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
       TableCell tc;
       foreach (FileSystemAccessRule r in auth)
       {
           TableRow tr = new TableRow();
           
           tc = new TableCell();
           tc.Text = r.AccessControlType.ToString(); 
           tr.Cells.Add(tc);
           tc = new TableCell();
           tc.Text = r.IdentityReference.Value; 
           tr.Cells.Add(tc);
           tc = new TableCell();
           tc.Text = r.InheritanceFlags.ToString();
           tr.Cells.Add(tc);
           tc = new TableCell();
           tc.Text = r.IsInherited.ToString();
           tr.Cells.Add(tc);
           tc = new TableCell();
           tc.Text = r.PropagationFlags.ToString();
           tr.Cells.Add(tc);
           tc = new TableCell();
           tc.Text = r.FileSystemRights.ToString();
           tr.Cells.Add(tc);
           Table1.Rows.Add(tr);
       }
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       File Owner:
             <asp:Label ID="Label1" runat="server" Text="Label" />        
       
       Access Rules:
<asp:Table ID="Table1" runat="server" CellPadding="2" GridLines="Both"> <asp:TableRow> <asp:TableHeaderCell>Control Type</asp:TableHeaderCell> <asp:TableHeaderCell>Identity</asp:TableHeaderCell> <asp:TableHeaderCell>Inheritance Flags</asp:TableHeaderCell> <asp:TableHeaderCell>Is Inherited</asp:TableHeaderCell> <asp:TableHeaderCell>Propagation Flags</asp:TableHeaderCell> <asp:TableHeaderCell>File System Rights</asp:TableHeaderCell> </asp:TableRow> </asp:Table>
   </form>

</body> </html></source>


Access Control List information (VB)

   <source lang="csharp">

<%@ Page Language="VB" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Security.AccessControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim sec As System.Security.AccessControl.FileSecurity = System.IO.File.GetAccessControl(Server.MapPath("TextFile.txt"))
       
       Me.Label1.Text = sec.GetOwner(System.Type.GetType("System.Security.Principal.NTAccount")).Value
     
       Dim auth As AuthorizationRuleCollection = sec.GetAccessRules(True, True, System.Type.GetType("System.Security.Principal.NTAccount"))
       Dim tc As TableCell
       For Each r As FileSystemAccessRule In auth
       
           Dim tr As New TableRow()
           
           tc = New TableCell()
           tc.Text = r.AccessControlType.ToString()
           tr.Cells.Add(tc)
           tc = New TableCell()
           tc.Text = r.IdentityReference.Value
           tr.Cells.Add(tc)
           tc = New TableCell()
           tc.Text = r.InheritanceFlags.ToString()
           tr.Cells.Add(tc)
           tc = New TableCell()
           tc.Text = r.IsInherited.ToString()
           tr.Cells.Add(tc)
           tc = New TableCell()
           tc.Text = r.PropagationFlags.ToString()
           tr.Cells.Add(tc)
           tc = New TableCell()
           tc.Text = r.FileSystemRights.ToString()
           tr.Cells.Add(tc)
           Table1.Rows.Add(tr)
       Next
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       File Owner:
             <asp:Label ID="Label1" runat="server" Text="Label" />        
       
       Access Rules:
<asp:Table ID="Table1" runat="server" CellPadding="2" GridLines="Both"> <asp:TableRow> <asp:TableHeaderCell>Control Type</asp:TableHeaderCell> <asp:TableHeaderCell>Identity</asp:TableHeaderCell> <asp:TableHeaderCell>Inheritance Flags</asp:TableHeaderCell> <asp:TableHeaderCell>Is Inherited</asp:TableHeaderCell> <asp:TableHeaderCell>Propagation Flags</asp:TableHeaderCell> <asp:TableHeaderCell>File System Rights</asp:TableHeaderCell> </asp:TableRow> </asp:Table>
   </form>

</body> </html></source>


Adding a rule to the Access Control List (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
       System.Security.AccessControl.FileSecurity sec =
        System.IO.File.GetAccessControl(Server.MapPath("Data.txt"));
       sec.AddAccessRule(
                new System.Security.AccessControl.FileSystemAccessRule(
                         @"DEMOXP\TestUser",
                         System.Security.AccessControl.FileSystemRights.FullControl,
                         System.Security.AccessControl.AccessControlType.Allow
                         )
                );
       File.SetAccessControl(Server.MapPath("TextFile.txt"), sec);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>


Adding a rule to the Access Control List (VB)

   <source lang="csharp">

<%@ Page Language="VB" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim sec As System.Security.AccessControl.FileSecurity = _
        System.IO.File.GetAccessControl(Server.MapPath("Data.txt"))
       sec.AddAccessRule( _
                New System.Security.AccessControl.FileSystemAccessRule( _
                         "DEMOXP\TestUser", _
                         System.Security.AccessControl.FileSystemRights.FullControl, _
                         System.Security.AccessControl.AccessControlType.Allow _
                         ) _
                )
       File.SetAccessControl(Server.MapPath("Data.txt"), sec)
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>


Removing the rule from the Access Control List (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
       System.Security.AccessControl.FileSecurity sec =
        File.GetAccessControl(Server.MapPath("Data.txt"));
       sec.RemoveAccessRule(
                new System.Security.AccessControl.FileSystemAccessRule(
                         @"DEMOXP\TestUser",
                         System.Security.AccessControl.FileSystemRights.FullControl,
                         System.Security.AccessControl.AccessControlType.Allow)
                );
       File.SetAccessControl(Server.MapPath("Data.txt"), sec);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>


Removing the rule from the Access Control List (VB)

   <source lang="csharp">

<%@ Page Language="VB" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim sec As System.Security.AccessControl.FileSecurity = _
        System.IO.File.GetAccessControl(Server.MapPath("Data.txt"))
       sec.RemoveAccessRule( _
                New System.Security.AccessControl.FileSystemAccessRule( _
                         "DEMOXP\TestUser", _
                         System.Security.AccessControl.FileSystemRights.FullControl, _
                         System.Security.AccessControl.AccessControlType.Allow _
                         ) _
                )
       File.SetAccessControl(Server.MapPath("Data.txt"), sec)
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>