Showing posts with label System.AccessControl. Show all posts
Showing posts with label System.AccessControl. Show all posts

Thursday, 10 May 2012

Screen scrapping means to get other sites data from given URL.
You can do this using the HttpWebRequest and HttpWebResponse classes to screen scrape, you can use the following code to build a Web page that will serve as a simple Web browser. You also learn how to display another Web page inside of yours using an HttpWebRequest. In this example, you scrape the "http://msdn.microsoft.com/en-US/" URL and display it in a panel on your Web page.
For this example we are using System.Net and System.IO Namespaces.

ASPX Code : 
 <asp:Panel runat="server" ID="pnlScreen" ScrollBars=Auto
Width="800px" Height="500px"></asp:Panel>

C# Example :
Uri url = new Uri("http://msdn.microsoft.com/en-US/");
if (url.Scheme == Uri.UriSchemeHttp)
{
    //Create Request Object
    HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    //Set Request Method
    objRequest.Method = WebRequestMethods.Http.Get;
    //Get response from requested url
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    //Read response in stream reader
    StreamReader reader = new StreamReader(objResponse.GetResponseStream());
    string tmp = reader.ReadToEnd();
    objResponse.Close();
    //Set response data to container
    this.pnlScreen.GroupingText = tmp;
}

The HttpWebRequest to the "http://msdn.microsoft.com/en-US/" page returns a string containing the scraped HTML. The sample assigns the value of this string to the GroupingText property of the Panel control. When the final page is rendered, the browser renders the HTML that was scraped as literal content on the page.

VB.net Example :
Dim url As New Uri("http://msdn.microsoft.com/en-US/")
If url.Scheme = Uri.UriSchemeHttp Then
    'Create Request Object
    Dim objRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    'Set Request Method
    objRequest.Method = WebRequestMethods.Http.[Get]
    'Get response from requested url
    Dim objResponse As HttpWebResponse = DirectCast(objRequest.GetResponse(), HttpWebResponse)
    'Read response in stream reader
    Dim reader As New StreamReader(objResponse.GetResponseStream())
    Dim tmp As String = reader.ReadToEnd()
    objResponse.Close()
    'Set response data to container
    Me.pnlScreen.GroupingText = tmp
End If
Output :
(To view original image , click on image)

Monday, 23 April 2012

To remove rule you can use RemoveAccessRule methods.
Here are sample Example.

C# Example :
        string strFilePath = "D:\\Others\\bookmarks.html";

        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(strFilePath);

        sec.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

        System.IO.File.SetAccessControl(strFilePath, sec);

If you open the file Properties dialog again, you see that the user has been removed from the Access
Control List.

Here are converted code for vb.net with automated tool.
        string strFilePath = "D:\Others\bookmarks.html";

        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(strFilePath);

        sec.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

        System.IO.File.SetAccessControl(strFilePath, sec);

Saturday, 21 April 2012

There is a situations where you want to modify the ACL lists.
In this example, you give a specific user explicit Full Control rights over the file.
You can use either an existing user or create a new test User account .

C# Example :
        string strFilePath = "D:\\Others\\bookmarks.html";

        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(strFilePath);

        sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"DEMO\TestUser",System.Security.AccessControl.FileSystemRights.FullControl,System.Security.AccessControl.AccessControlType.Allow));

        System.IO.File.SetAccessControl(strFilePath, sec);

The example starts with getting the collection of existing security settings for the file.
After that , using the AddAccessRule method, a new FileSystemAccessRule is created and added to the files collection of security settings.
Creating a new FileSystemAccessRule requires you to provide three constructor parameters, the user you want to assign this rule to (provided in DOMAIN\USERNAME format), the rights
you want to assign to this rule, and the AccessControlType you want to give this rule.
You can specify multiple rights to assign to the rule by using a bitwise Or operator,
as shown in the following Example:
sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"DEMO\TestUser", System.Security.AccessControl.FileSystemRights.Read | System.Security.AccessControl.FileSystemRights.Write, System.Security.AccessControl.AccessControlType.Allow));

These rules allows the TestUser account to read or write to the  file system asset.
You can also deny a specific user these rights by changing the AccessControlType value to Deny.

After execute code take a look at the Security tab in the file’s Properties dialog, and you should see
that the user has been added to the Access Control List for allowed rights.


Here are converted code for vb.net with automated tool
         Dim strFilePath As String =  "D:\\Others\\bookmarks.html"

         Dim sec As System.Security.AccessControl.FileSecurity =  System.IO.File.GetAccessControl(strFilePath)

        sec.AddAccessRule(New System.Security.AccessControl.FileSystemAccessRule("Everyone",System.Security.AccessControl.FileSystemRights.FullControl,System.Security.AccessControl.AccessControlType.Allow))
        sec.AddAccessRule(New System.Security.AccessControl.FileSystemAccessRule("Everyone", System.Security.AccessControl.FileSystemRights.Read | System.Security.AccessControl.FileSystemRights.Write, System.Security.AccessControl.AccessControlType.Allow))

        System.IO.File.SetAccessControl(strFilePath, sec)

Thursday, 19 April 2012

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())