【问题标题】:How to hash image source url in ASP.NET如何在 ASP.NET 中对图像源 URL 进行哈希处理
【发布时间】:2012-04-12 11:06:47
【问题描述】:

您好,我想发送带有图像源哈希的 url。怎么做?

代码:

Image img_ = new Image();          
img_.ImageUrl = "ShowImageInRuntime.aspx?FileName=C://Images//Image.jpg";

我不想在页面中看到我的本地路径。谢谢您的帮助

【问题讨论】:

  • 您需要将本地路径存储在某处。您是否有存储应用程序的存储空间,您可以在其中存储路径并查找它们,例如数据库?如果有一个小的固定集,您可以在必要时将路径硬编码到您的应用程序中。否则,您必须提供加密路径并在 ShowImageAtRuntime 中对其进行解密(然后在使用前进行验证!)尽管用户可能会构建自己的加密路径。
  • 只要用于加密的秘密仍然安全,用户可能无法通过随机更改字符和进行有根据的猜测来创建有效的加密字符串。

标签: javascript asp.net url hash


【解决方案1】:

您需要在发送之前执行路径加密和 base 64 编码,并在处理程序中执行相反的操作。

其他选项是维护一个映射 XML,其中 XML 将包含路径和相应的标识符。在您的应用程序中获取具有路径的节点并选择 id 并将其发送到 url。在处理程序中执行相反的操作以获取路径。

更新:添加示例代码

以 GUID 作为 ID 和对应文件名的 XML 文件。通过使用 guid,人们无法轻易猜出随机 id。

<?xml version="1.0" encoding="utf-8" ?>
<mappings>
  <map id="485D9075B9CA4d8d9DBA1AD9CD09FC13" fileName="c:\images\image1.jpg"/>
  <map id="475D4A22B1FA4eda8234007BD327D7B9" fileName="c:\images\image2.jpg"/>
  <map id="77623BF3094440c49AC65CEF76D1B824" fileName="c:\images\image3.jpg"/>
</mappings>

使用 LINQ to XML

public class ImageMapper
{
    private XDocument Document { get; set; }

    public ImageMapManager()
    {
        Document = XDocument.Load("Map.xml");
    }

    public string GetFileNameFromId(string id)
    {
        var result = from node in Document.Descendants("map")
                     where node.Attribute("id").Value.Equals(id, StringComparison.InvariantCultureIgnoreCase)
                     select node.Attribute("fileName").Value;
        return (result == null || result.Count() == 0) ? null : result.ElementAt(0);
    }
    public string GetIdFromFileName(string fileName)
    {
        var result = from node in Document.Descendants("map")
                     where node.Attribute("fileName").Value.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)
                     select node.Attribute("id").Value;
        return (result == null || result.Count() == 0) ? null : result.ElementAt(0);

    }
}

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多