Friday, 10 May 2013

Securing Images in ASP.NET c#

add new imageHandler.aspx

using System.IO;

protected void Page_Load(object sender, EventArgs e)
{
        string fileName = Request.QueryString["src"];
        long fileSize = 0;
        if (string.IsNullOrEmpty(fileName))
        {
            fileName = "empty.gif";
        }
        fileName = Server.MapPath("..\\images\\" + fileName);
        FileStream fileStream = default(FileStream);
        fileStream = new FileStream(fileName, FileMode.Open);
        fileSize = fileStream.Length;
        byte[] Buffer = new byte[Convert.ToInt32(fileSize) + 1];
        fileStream.Read(Buffer, 0, Convert.ToInt32(fileSize));
        fileStream.Close();
        Response.BinaryWrite(Buffer);
}

add new default.aspx
<img src="imageHandler.aspx?src=1.jpg" />

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...