禁止直接访问图像

发布于 2024-12-24 03:48:30 字数 479 浏览 1 评论 0原文

我正在制作一个小家庭相册,打算稍后向其他人开放以存储图像。

我将图像上传到 ~\images\,然后调整它们大小 3 次(普通视图...缩略图和小版本)并将它们移动到 ~\images\thumbs、~\images\normal、~\images\tiny,然后将原件移动到 ~\images\original。

如果用户知道文件名,他们只需转到 http://mysite.com/images/normal/filename.jpg< /a> 用于直接访问。

我不希望他们可以使用它。

那么,有没有办法更改 asp:Image 控件的 ImageUrl,而不是从不可访问的文件夹中读取?这会影响性能吗?我在想(不确定是否可能)将图像读入 Steam,并以某种方式设置 ImageUrl 或其他什么,以从流中读取?

希望您能帮忙。

I am making a little family photo album, with the intention to maybe open it to other people to store images later.

I upload the images to ~\images\, then resize them 3 times (Normal view ... thumbnail and a tiny version) and move them to ~\images\thumbs, ~\images\normal, ~\images\tiny and then move the original to ~\images\original.

If a user knows a file name, they can just goto http://mysite.com/images/normal/filename.jpg for direct access.

I'd prefer that not to be available to them.

So, is there a way to change the ImageUrl of the asp:Image control, to rather read from a non-accessable folder? And would this be a performance hit? I'm thinking something like (Not sure if it's possible) reading the image into s Steam, and somehow setting the ImageUrl or what ever, to read from the stream?

Hope you can assist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-12-31 03:48:30

答案是肯定的。您可以使用特殊格式的 img src ,它将图像直接嵌入到页面中。

您可以使用以下格式而不是 URL:

<img src="data:image/png;base64,..." />

其中 data 指定图像的 mime 类型,base64 之后的 ... 是图像的 base64 编码内容。

此行为由此 RFC 定义。

下面是一个示例:

在 HTML 中:

<img id="MyPicture" runat="server" border="0" />

在隐藏代码中:

    protected void Page_Load(object sender, EventArgs e)
    {
        MyPicture.Src = @"data:image/gif;base64," + EncodeFile(Server.MapPath(@"/images/test.gif"));
    }

    private string EncodeFile(string fileName)
    {
        return Convert.ToBase64String(File.ReadAllBytes(fileName));
    }

The answer is yes. You can use a special format of img src which will embed the image directly in the page.

Instead of the URL, you would use the following format:

<img src="data:image/png;base64,..." />

where data specifies the mime type of your image and the ... after base64, is the base64 encoded contents of the image.

This behavior is defined by this RFC.

Here is an example:

In the HTML:

<img id="MyPicture" runat="server" border="0" />

In the code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        MyPicture.Src = @"data:image/gif;base64," + EncodeFile(Server.MapPath(@"/images/test.gif"));
    }

    private string EncodeFile(string fileName)
    {
        return Convert.ToBase64String(File.ReadAllBytes(fileName));
    }
七月上 2024-12-31 03:48:30

我可以建议考虑类似的事情:

  1. 将图像保存到“siteroot/images/userid/”之类的文件夹中。
  2. 成功登录后,设置一个会话变量,以便其他页面知道当前用户已登录。 Session["user"] = youruser; 无论您在会话变量中存储什么对象,都包含有效的 youruser从数据库中提取的 .userID。
  3. 在站点根目录下的 Global.asax 文件中添加如下内容:(

未经测试的代码)

void Begin_Request(object sender, EventArgs e)
{
    //Protect /images/ folder for all requests
    if (Request.FilePath.Contains("images"))
    {
        YourUser user = Session["user"];

        if(user == null) {
            Server.Transfer("404.aspx"); //Folder not public
        }

        if (!Request.FilePath.Contains("images/" + user.userID.toString())){
            Server.Transfer("404.aspx"); //This is not current user's image folder
        }

    }
}

这应该有效地拒绝除各自“图像”文件夹的正确用户之外的所有人的访问,即使他们找出图像文件的直接路径。

如果有人有任何方法来改进这种方法,请随时发表评论,因为我很快就会在网站上实现这个想法,并且随时欢迎第二意见。

Might I suggest considering something like:

  1. Save images to a folder like "siteroot/images/userid/".
  2. Upon successful log in set a session variable so that other pages will know that the current user is logged in. Session["user"] = youruser; Whatever object you store in your session variable include a valid youruser.userID that is pulled from a database.
  3. In your Global.asax file on your site's root add something like:

(untested code)

void Begin_Request(object sender, EventArgs e)
{
    //Protect /images/ folder for all requests
    if (Request.FilePath.Contains("images"))
    {
        YourUser user = Session["user"];

        if(user == null) {
            Server.Transfer("404.aspx"); //Folder not public
        }

        if (!Request.FilePath.Contains("images/" + user.userID.toString())){
            Server.Transfer("404.aspx"); //This is not current user's image folder
        }

    }
}

this should effectively deny access to everyone but the correct user of their respective "images" folder, even if they figure out the direct path of the image files.

If anyone has any ways to improve this method feel free to comment as I will soon be implementing this idea in a site and second opinions are always welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文