Showing posts with label FileSecurity. Show all posts
Showing posts with label FileSecurity. 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)

Saturday, 28 April 2012

Now use the BinaryReader and BinaryWriter classes to read and write primitive types to a file.The BinaryWriter writes primitive objects in their native format, so in order to read them using the BinaryReader, you must select the appropriate Read method.
The Example shows you how to do that; in this case, you are writing a value from a number of different primitive types to the text file and then reading the same value.

Here are sample Example :

C# Example :
        ///////Write to a file
        System.IO.BinaryWriter binarywriter =new System.IO.BinaryWriter(System.IO.File.Create(MapPath
 ("BinaryFile.dat")));
        binarywriter.Write("This is a sample string.");
        binarywriter.Write(0x12346789abcdef);
        binarywriter.Write(0x12345678);
        binarywriter.Write('c');
        binarywriter.Write(1.5f);
        binarywriter.Write(1000.2m);
        binarywriter.Close();

        ////////Read from a file
        System.IO.BinaryReader binaryreader = new System.IO.BinaryReader(System.IO.File.Open
(MapPath("BinaryFile.dat"), System.IO.FileMode.Open));
        string strA = binaryreader.ReadString();
        long lngL = binaryreader.ReadInt64();
        int intI = binaryreader.ReadInt32();
        char chrC = binaryreader.ReadChar();
        float fltF = binaryreader.ReadSingle();
        decimal dclD = binaryreader.ReadDecimal();
        binaryreader.Close();


If you open this file in Notepad, you will see that the BinaryWriter has written the nonreadable binary data to the file. The BinaryReader provides a number of different methods for reading various kinds of Primitive types from the stream. In this Example, you use a different Read method for each primitive type that you Write to the file.

VB.net Example :
    Dim binarywriter As New System.IO.BinaryWriter(System.IO.File.Create(MapPath("binary.dat")))
    binarywriter.Write("a string")
    binarywriter.Write(&H12346789ABCDEF)
    binarywriter.Write(&H12345678)
    binarywriter.Write("c"c)
    binarywriter.Write(1.5F)
    binarywriter.Write(100.2D)
    binarywriter.Close()

    Dim binaryreader As New System.IO.BinaryReader(System.IO.File.Open(MapPath
     ("binary.dat"),System.IO.FileMode.Open))
    Dim a As String = binaryreader.ReadString()
    Dim l As Long = binaryreader.ReadInt64()
    Dim i As Integer = binaryreader.ReadInt32()
    Dim c As Char = binaryreader.ReadChar()
    Dim f As Double = binaryreader.ReadSingle()
    Dim d As Decimal = binaryreader.ReadDecimal()
    binaryreader.Close()

Friday, 27 April 2012

StreamReader and StreamWriter classes to write a string to a text file and then read the contents of that text file.

Here are Exmaple for Write and Read File.

C# Example :
         /////Write to a file
        System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(System.IO.File.Open
(MapPath("TextFile.txt"),System.IO.FileMode.OpenOrCreate));
        streamwriter.Write("This is a sample string");
        streamwriter.Close();

        //////Read from a file
        System.IO.StreamReader reader =new System.IO.StreamReader(System.IO.File.Open(MapPath
("TextFile.txt"),System.IO.FileMode.Open));
        string tmp = reader.ReadToEnd();
        reader.Close();


When you create a StreamReader, you must pass an existing stream instance as a constructor
parameter. The reader uses this stream as its underlying data source.
In this sample, you use the File class’s Open method to open a writable FileStream for your StreamWriter.

Also notice that you no longer have to deal with byte arrays. The StreamReader takes care of converting the data to a type that’s more user-friendly than a byte array. In this example, you are using the ReadToEnd method to read the entire stream and convert it to a string.The StreamReader provides a number of different methods for reading data that you can use depending on exactly how you want to read the data, from reading a single character using the Read method, to reading the entire file using the ReadToEnd method.

VB.net Example :
    /////Write to a file
    Dim streamwriter As New System.IO.StreamWriter(System.IO.File.Open(MapPath
                     ("TextFile.txt"),System.IO.FileMode.Open))
    streamwriter.Write("This is a string")
    streamwriter.Close()

    //////Read from a file
    Dim reader As New System.IO.StreamReader(System.IO.File.Open(MapPath
                   ("TextFile.txt"),System.IO.FileMode.Open))
    Dim tmp As String = reader.ReadToEnd()
    reader.Close()

Thursday, 26 April 2012

There is a situation where you want to write data in Memory Stream.
You can write to the stream by encoding a string containing the information you want to write to a byte array and then using the stream’s Write method to write the byte array to the MemoryStreams.
The Close method also calls Flush internally to commit the data to the data store.

Here are sample Example
C# Example :
    byte[] data = System.Text.Encoding.ASCII.GetBytes("This is a sample string");
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    ms.Write(data, 0, data.Length);
    ms.Close();

VB.net Example :
    Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes("This is a sample string")
    Dim ms As New System.IO.MemoryStream()
    ms.Write(data, 0, data.Length)
    ms.Close()

Wednesday, 25 April 2012

Any type of I/O operation you are performing in .NET, if you want to read or write data you eventually use a stream of some type. Streams are the basic mechanism. .NET uses to transfer data to and from its underlying source, it will be a file, communication pipe, or TCP/IP socket. The Stream class provides the basic functionality to read and write I/O data, but because the Stream class is marked as abstract, you most likely need to use one of the several classes derived from Stream. Each Stream derivation is specialized to make transferring data from a specific source easy.
Here is Table to lists some of the classes derived from the Stream class.

TABLE :

CLASS DESCRIPTION
System.IO.FileStream Reads and writes files on a file system, as well as other file related operating system handles (including pipes, standard input, standard output, and so on).
System.IO.MemoryStream Creates streams that have memory as a backing store instead of a disk or a network connection. This can be useful in eliminating the need to write temporary files to disk or to store binary information in a database.
System.IO.UnmanagedMemoryStream Supports access to unmanaged memory using the existing stream-based model and does not require that the contents in the unmanaged memory be copied to the heap.
System.IO.BufferedStream Extends the Stream class by adding a buffering layer to read and write operations on another stream. The stream performs reads and writes in blocks (4096 bytes by default), which can result in improved efficiency.
System.Net.Sockets.NetworkStream Implements the standard .NET Framework stream to send and receive data through network sockets. It supports both synchronous and asynchronous access to the network data stream.
System.Security.Cryptography.CryptoStream Enables you to read and write data through cryptographic transformations.
System.IO.Compression.GZipStream Enables you to compress data using the GZip data format.
System.IO.Compression.DeflateStream Enables you to compress data using the Deflate algorithm.
System.Net.Security.NegotiateStream Uses the Negotiate security protocol to authenticate the client,and optionally the server, in client-server communication.
System.Net.Security.SslStream Necessary for client-server communication that uses the Secure Socket Layer (SSL) security protocol to authenticate the server and optionally the client.

Tuesday, 24 April 2012

There are many times you want to read text file.
There are many ways to read files. One way is to use FileStream class.
Here are sample example to read the file and display it's content on page.

C# Example :
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("TextFile.txt"),System.IO.FileMode.Open);
        byte[] data = new byte[fs.Length];
        fs.Read(data, 0, (int)fs.Length);
        fs.Close();
        Response.Write( System.Text.ASCIIEncoding.Default.GetString(data));

In this example creating a byte array the length of the stream, using the Length property to properly size the array, and then passing it to the Read method.
The Read method fills the byte array with the stream data, in this case reading the entire stream into the byte array. If you want to read only a chunk of the stream or to start at a specific point in the stream simply change the value of the parameters you pass to the Read method.

Streams must always be explicitly closed in order to release the resources they are using, which in this case is the file. Failing to explicitly close the stream can cause memory leaks, and it may also deny other users and applications access to the resource.

VB.net Example :
        Dim fs As New System.IO.FileStream(Server.MapPath("TextFile.txt"), System.IO.FileMode.Open)
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, fs.Length)
        fs.Close()
        Response.Write(  ASCIIEncoding.Default.GetString(data) )

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