Ahmad Ahsan
Ahmad Ahsan

Reputation: 187

Secret image path in WebForms application

I am using Visual Studio 2008 (.NET framework 3.5) and currently I am uploading images to my ASP.NET WebForms application in C#. If some one copies my image url and opens in new window/tab then complete directory structure of my site is revealed to the user, some thing like http://domain.name/images/companylogo/logo599.jpg
I want to hide my directory structure and show image path something like:

http://www.zameen.com/common/resize.php?img=4/303088.jpg&d=&w=250&h=147&r=1 http://www.zameen.com/common/resize.php?img=7/165/bhurbun_continental_apartments_380.jpg&d=250&w=250&h=180&r=1 http://www.zameen.com/common/resize.php?img=2/156050.jpg&d=80&m=1

To hide my site directory structure I configured the imageshack API to upload images but requirements are like the path above. I am not getting how to manage the path format stated above.

Upvotes: 0

Views: 1383

Answers (4)

Ahmad Ahsan
Ahmad Ahsan

Reputation: 187

Finally I found something that somehow meets my requirements. Check the link below, might be helpful to someone. http://blog.kurtschindler.net/using-httphandlers-to-serve-image-files/

Upvotes: 0

शेखर
शेखर

Reputation: 17614

This is not an exact implementation this is just an idea. Steps to get that.

  1. Create a Table (database) or xml file to create a unique id and path for the image.
  2. like ID=1 and path="~/folder/file.jpg".
  3. Create a generic handler which will provide the image in response.
  4. Use the image path like this if you have to call the image with code 1 "imagehandler?id=1"

Image Handler could be as below

 public class ImageHandler : IHttpHandler
 {
    public void ProcessRequest(HttpContext context)
    {
        var imageId = QueryString.getValueOf("ID");
        var imagePath="";//calculate for database or xml file
        var originalImage = Image.FromFile (context.Server.MapPath(imagePath);
        originalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);  
        context.Response.ContentType = "image/gif"; //or any type
     }
  }

and give the image path to handler with image id.

Some useful links

  1. http://www.codeproject.com/Articles/126421/Image-Handling-In-ASP-NET
  2. http://www.dotnetperls.com/ashx

Upvotes: 0

user2316116
user2316116

Reputation: 6814

The easiest way is to create an .ashx handler that will serve requests to the images. Instead of calling

http://domain.name/images/companylogo/logo599.jpg

you could use

http://domain.name/photo.ashx?id=599

Example of the code: http://www.dotnetperls.com/ashx

If you use imageshack API or any other stuff to resize, etc. or images are located on remote server then you can code your handler as shown at C# - Loading remote image and sending to browser using .ashx file

Upvotes: 0

Christoph Fink
Christoph Fink

Reputation: 23103

You need to place you images in a folder not accessible by the user and serve the images via a handler. To make the folder inaccessible you can put a web.config file in that folder with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="DenyAll" path="*.*" verb="*" 
                 type="System.Web.HttpForbiddenHandler" />
        </handlers>
    </system.webServer>
</configuration>

Then add a Generic Handler (*.ashx) to your project andd do something like:

public class FileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var path = GetPath(context.Request.Params["Id"]);
        context.Response.ContentType = "image/jpeg";
        context.Response.WriteFile(path);
    }
}

Upvotes: 1

Related Questions