嗨,
我有一个类似于"http://project/page1.aspx?userID=5“的查询字符串。如果手动更改了'userID‘参数,则不会执行该操作。这怎么可能呢?
发布于 2010-05-11 20:07:46
大家好,谢谢你们的帮助..。我从其他网站得到了一些不同的解决方案。我不知道最好的解决方案。也就是说,使用加密和解密算法对值进行编码...示例代码是这样写的……
<a href='Page1.aspx?UserID=<%= HttpUtility.UrlEncode(TamperProofStringEncode("5","F44fggjj")) %>'>
Click Here</a> <!--Created one anchor tag and call the function for TamperProofStringEncode--> private string TamperProofStringEncode(string value, string key)
{
System.Security.Cryptography.MACTripleDES mac3des = new System.Security.Cryptography.MACTripleDES();
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value)) + "-" + Convert.ToBase64String(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)));
}在'Page1‘的页面加载中,调用解码算法对查询字符串进行解码
try
{
string DataString = TamperProofStringDecode(Request.QueryString["UserID"], "F44fggjj");
Response.Write(DataString);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}private string TamperProofStringDecode(string value, string key)
{
string dataValue = "";
string calcHash = "";
string storedHash = "";
System.Security.Cryptography.MACTripleDES mac3des = new System.Security.Cryptography.MACTripleDES();
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
try
{
dataValue = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[0]));
storedHash = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[1]));
calcHash = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));
if (storedHash != calcHash)
{
//'Data was corrupted
throw new ArgumentException("Hash value does not match");
// 'This error is immediately caught below
}
}
catch (Exception ex)
{
throw new ArgumentException("Invalid TamperProofString");
}
return dataValue;
} 发布于 2010-05-11 17:24:44
这听起来像是一个奇怪的要求。您是否正在尝试实现某种自主开发的安全性?如果是这样,你真的不应该这么做。
无论如何,你可以这样做的一种方法是获取整个url http://project/page1.aspx?userID=5并计算它的md5 sum。然后将md5和附加到最终的url,例如http://project/page1.aspx?userID=5&checksum=YOURCALCULATEDMD5SUM。然后,在page1.aspx中,您必须验证校验和参数是否正确。
然而,这种方法相当天真,任何人都不一定要花很长时间才能弄清楚你所使用的算法。如果他们这样做了,他们就可以“轻松”地更改用户did并自己计算md5和。更健壮的方法是使用只有您才能访问的密钥对校验和进行加密。但我不得不再次质疑您想要这样做的动机,因为存在其他更好的安全解决方案。
发布于 2010-05-11 17:28:24
你不能这么做。
HTTP请求中的任何内容(包括URL、查询字符串、cookies等)在客户的控制下,很容易被伪造。
这就是为什么将有效内容列入白名单很重要,因为除了您提示接收的内容之外,客户端可以随意添加任何它喜欢的内容。
https://stackoverflow.com/questions/2809383
复制相似问题