关于asp.net label值的问题(70分) 请注意我最后的回复!贴子见:http://topic.csdn.net/u/20110104/11/233a8c9a-cb73-4a7a-b246-078a6cfda9c5.html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然不可行,原因那贴里已经重复过很多遍了label生成的是span,span不会回传,所以无论你客户端怎么改,服务器端的值还是不会变的 这个道理我当然知道。我是说这个状态可能保存在__VIEWSTATE中。修改它也一样呀。总不可能保存在服务器的内存中呀? 这个好像不是保存在那个隐藏域中的,只保存在服务器端的内存中。你给Label.Text一个很长的文本值看看_VIEWSTATE中value的变化,并没有变长。 我刚才试了下很长的文本,_VIEWSTATE里确实有保存,说明确实也存在那个隐藏域中的,但这是编码加密过的,客户端很难做手脚。 你担心这个的话直接把VIEWSTATE加密不就可以了吗还有你不觉得你的逻辑有点怪吗?你与其担心数据被非法篡改不如考虑一下怎样完善数据验证 默认是不加密的,只是进行了Base64编码 ViewState 解析器值是可以解析出来的,你也可以通过JS修改ViewState 但是要一一对应的找出来修改,好像有点困难, viewstate,回传会验证数据重新加载数据 给你贴个Page父类:using System;using System.IO;using System.Linq;using System.Runtime.Serialization.Formatters.Binary;using System.Web.UI;public class VPage : Page{ static private DirectoryInfo _Dir; private static DirectoryInfo Dir { get { if (_Dir == null) { _Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data")); if (!_Dir.Exists) _Dir.Create(); _Dir = new DirectoryInfo(Path.Combine(_Dir.FullName, "ViewState")); if (!_Dir.Exists) _Dir.Create(); } return _Dir; } } protected override object LoadPageStateFromPersistenceMedium() { PageStatePersister ps = this.PageStatePersister; ps.Load(); if (ps.ControlState != null) ps.ControlState = 反序列化对象((string)ps.ControlState); if (ps.ViewState != null) ps.ViewState = 反序列化对象((string)ps.ViewState); return new Pair(ps.ControlState, ps.ViewState); } protected override void SavePageStateToPersistenceMedium(object state) { PageStatePersister ps = this.PageStatePersister; if (state is Pair) { ps.ControlState = ((Pair)state).First; ps.ViewState = ((Pair)state).Second; } else ps.ViewState = state; if (ps.ControlState != null) ps.ControlState = 序列化对象(ps.ControlState); if (ps.ViewState != null) ps.ViewState = 序列化对象(ps.ViewState); ps.Save(); } private object 反序列化对象(string stateID) { if (stateID == null) return null; object state = Cache[stateID]; string fname = Path.Combine(Dir.FullName, stateID); if (state == null) using (var stream = File.OpenRead(fname)) state = new BinaryFormatter().Deserialize(stream); this.Page.SaveStateComplete += (s, e) => //当发生异常时,此方法不会执行,因此可以保证不会误删ViewState文件 { Cache.Remove(stateID); File.Delete(fname); //这个方法恰好可以禁止浏览器端的“刷新”。 }; return state; } static long seed1 = DateTime.Now.Ticks; static ulong seed2 = 0; static object lockflag = new object(); private string 序列化对象(object obj) { lock (lockflag) { string stateID = seed1.ToString() + "_" + (seed2++).ToString(); Cache.Insert(stateID, obj, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero); var stream = new MemoryStream(); new BinaryFormatter().Serialize(stream, obj); File.WriteAllBytes(Path.Combine(Dir.FullName, stateID), stream.ToArray()); return stateID; } } static VPage() { var et = DateTime.Now.AddMinutes(-30); Dir.GetFiles() .Where(f => f.LastAccessTime < et) .ToList() .ForEach(f => { try { f.Delete(); } catch { } }); }} 索引超出了数组界限 索引和长度必须引用该字符串内的位置。 C#与EXCEL文件导入导出,报错,怎么解决,大家帮忙啊! 关于页面缓存的问题 我重新安装了IIS,但是依然无法浏览ASP类型的网页,为什么? 连接数据库问题?入门级问题 如何分析html文件,得到我想要的文本信息? 如何在datagrid绑定后修改datagrid 的header.text的值? DataGrid能否在最前面绑定一序号列,显示时自增,而其与数据库无关 关于图片读取后的问题 怎么把dataset的查询结果显示在文本框内 给动态添加的textbox加TextChanged事件为什么不触发?
label生成的是span,span不会回传,所以无论你客户端怎么改,服务器端的值还是不会变的
这个道理我当然知道。我是说这个状态可能保存在__VIEWSTATE中。修改它也一样呀。总不可能保存在服务器的内存中呀?
还有你不觉得你的逻辑有点怪吗?
你与其担心数据被非法篡改不如考虑一下怎样完善数据验证
ViewState 解析器
值是可以解析出来的,你也可以通过JS修改ViewState 但是要一一对应的找出来修改,好像有点困难,
重新加载数据
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.UI;public class VPage : Page
{
static private DirectoryInfo _Dir; private static DirectoryInfo Dir
{
get
{
if (_Dir == null)
{
_Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"));
if (!_Dir.Exists)
_Dir.Create();
_Dir = new DirectoryInfo(Path.Combine(_Dir.FullName, "ViewState"));
if (!_Dir.Exists)
_Dir.Create();
}
return _Dir;
}
} protected override object LoadPageStateFromPersistenceMedium()
{
PageStatePersister ps = this.PageStatePersister;
ps.Load();
if (ps.ControlState != null)
ps.ControlState = 反序列化对象((string)ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = 反序列化对象((string)ps.ViewState);
return new Pair(ps.ControlState, ps.ViewState);
} protected override void SavePageStateToPersistenceMedium(object state)
{
PageStatePersister ps = this.PageStatePersister;
if (state is Pair)
{
ps.ControlState = ((Pair)state).First;
ps.ViewState = ((Pair)state).Second;
}
else
ps.ViewState = state;
if (ps.ControlState != null)
ps.ControlState = 序列化对象(ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = 序列化对象(ps.ViewState);
ps.Save();
} private object 反序列化对象(string stateID)
{
if (stateID == null)
return null; object state = Cache[stateID];
string fname = Path.Combine(Dir.FullName, stateID);
if (state == null)
using (var stream = File.OpenRead(fname))
state = new BinaryFormatter().Deserialize(stream);
this.Page.SaveStateComplete += (s, e) => //当发生异常时,此方法不会执行,因此可以保证不会误删ViewState文件
{
Cache.Remove(stateID);
File.Delete(fname); //这个方法恰好可以禁止浏览器端的“刷新”。
};
return state;
} static long seed1 = DateTime.Now.Ticks;
static ulong seed2 = 0;
static object lockflag = new object(); private string 序列化对象(object obj)
{
lock (lockflag)
{
string stateID = seed1.ToString() + "_" + (seed2++).ToString();
Cache.Insert(stateID, obj, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
var stream = new MemoryStream();
new BinaryFormatter().Serialize(stream, obj);
File.WriteAllBytes(Path.Combine(Dir.FullName, stateID), stream.ToArray());
return stateID;
}
} static VPage()
{
var et = DateTime.Now.AddMinutes(-30);
Dir.GetFiles()
.Where(f => f.LastAccessTime < et)
.ToList()
.ForEach(f =>
{
try
{
f.Delete();
}
catch { }
});
}
}