Home > OS >  How can I make the data image link in ASP.NET temporary
How can I make the data image link in ASP.NET temporary

Time:03-17

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

CodePudding user response:

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

CodePudding user response:

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

  • Related