[SOLVED] How can I make the data image link in ASP.NET temporary

Issue

We have an old ASP.NET application. We have an image element on one of the pages. For the security purpose we would like to stream the image instead of a direct reference. However, after the data link is generated from the stream, it appears to be permanent. Even after the application is closed, the data link is still alive.

Is there a way to make this link invalid when out of session, to somehow invalidate it, by maybe emptying a stream?

Please let me know

Thank you in advance

    MemoryStream ms = new MemoryStream();
    string physicalPath = "some path of an image";

    using (FileStream fs = new FileStream(physicalPath, FileMode.Open, FileAccess.Read))
    {
        fs.CopyTo(ms);
    }

    img.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);

Solution

Found a solution

Changes the above code to

 Session["ImageStream"] = true;
 img.ImageUrl = "MyImage.aspx";

Where the code for the MyImage.aspx is

   protected override void Page_Load(object sender, EventArgs e)
   {
        if (Session["ImageStream"] != null && (bool)Session["ImageStream"])
        {
            string somePath = "Some path";
            byte[] imageBytes = File.ReadAllBytes(somePath);
            Response.BinaryWrite(imageBytes);
        }
  }

Answered By – James

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *