skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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);
            }
      }
    

  2. You may consider placing a javascript code that change the element "src" attribute when the session is expired.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search