2012-04-12 88 views
0

嗨,我想用圖像源哈希值發送URL。如何做到這一點?如何在ASP.NET中對圖像源URL進行哈希處理

代碼:

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

我不想看到我的page.Thx本地路徑的幫助

+0

你需要存儲的本地路徑的某處。您的應用程序是否有存儲空間,可以存儲路徑並查找它們一個數據庫?如果有一個小的固定設置,您可以在必要時將路徑硬編碼到您的應用程序中。否則,您將不得不提供加密路徑並在ShowImageAtRuntime中進行解密(然後在使用前進行驗證!),但用戶可能能夠構建自己的加密路徑存在危險。 – Rup 2012-04-12 11:11:24

+0

只要用於加密的祕密保持安全,用戶可能無法通過隨機更改字符並進行有根據的猜測來創建有效的加密字符串。 – Ramesh 2012-04-12 11:14:40

回答

2

你需要或者在發送之前進行路徑和基地64編碼加密和做反向在處理程序中。

其他選項是維護映射XML,其中XML將包含路徑和相應的標識符。在您的應用程序中獲取具有路徑的節點並選擇該ID並將其附加到URL。在處理程序中做相反的操作來獲取路徑。

UPDATE:添加樣品代碼GUID

XML文件作爲ID和對應的文件名。通過使用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到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); 

    } 
} 
相關問題