什么是视图状态?它是如何编码的?是否加密?谁使用 ViewState?
什么是视图状态?它是如何编码的?是否加密?谁使用 ViewState?
What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
视图状态是一种哈希映射(或者至少您可以这样认为),ASP.NET 使用它来存储有关页面的所有临时信息 - 例如每个选择框中当前选择的选项、其中的值在每个文本框中,打开哪个面板等。您还可以使用它来存储任何任意信息。
整个地图被序列化和
加密编码,并保存在一个隐藏变量中,每当您在需要服务器往返的页面上执行任何操作时,该变量都会发布回服务器。您可以通过这种方式从服务器代码访问控件上的值。如果您更改服务器代码中的任何值,则会在视图状态中进行该更改并将其发送回浏览器。不过,请注意在视图状态中存储了多少信息……它很快就会变得臃肿,并且每次到服务器和返回的传输速度都很慢。
至于加密,我不知道它有多强,但它肯定不容易被人类阅读。不过,我不会将其用于敏感信息。正如评论中所指出的,它根本没有加密。只是基本编码,很容易逆转。View state is a kind of hash map (or at least you can think of it that way) that ASP.NET uses to store all the temporary information about a page - like what options are currently chosen in each select box, what values are there in each text box, which panel are open, etc. You can also use it to store any arbitrary information.
The entire map is serialized and
encryptedencoded and kept in a hidden variable that's posted back to the server whenever you take any action on the page that requires a server round trip. This is how you can access the values on the controls from the server code. If you change any value in the server code, that change is made in the view state and sent back to the browser.Just be careful about how much information you store in the view state, though... it can quickly become bloated and slow to transfer each time to the server and back.
As for encryption, I dont' know how strong it is, but its sure not easily human readable. I wouldn't use it for sensitive information, though.As pointed out in the comments, it's not encrypted at all. Just base encoded, which easily reversible.如果您确实想理解 ViewState(不仅仅是它的用途),那么您可能需要阅读这篇精彩的文章(不幸的是,我不是该文章的作者:-)。
但请注意,它有点过时了,但仍然是一本很好的读物。
If you really want to understand ViewState (not just what it is used for), then you may want to read this fabulous article (which I, unfortunately, am not the author of :-).
Beware, though, it is a bit dated, but still a very good read.
请允许我与大家分享我今天学到的东西。
什么是视图状态?
如果我有这样的内容:
默认情况下,页面的视图状态放置在名为 __VIEWSTATE 的隐藏表单字段中。
了解更多内容
它是如何编码的?是否加密?
默认情况下,ViewState 已编码但未加密。让我们采用前面的输入类型值来运行以下代码:
上述代码的输出将是
? -1616687229 UserName Shubh DasguptaPassword IAmAPassworddd??O??J(Oc|f?X?? b???=??}+)?f"
如果您详细阅读了我之前提到的文章,您会发现会提出“Cost Of ViewState”,它写得清晰漂亮:
自己尝试一下。 下载示例
Allow me to share with you what I learned today.
What is ViewState?
If I have something like this:
The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE.
Read More
How is it encoded? Is it encrypted?
ViewState is Encoded and not Encrypted by default. Lets take the previous input type value are run the below code:
The output for the above code will be
?-1616687229UserNameShubh DasguptaPassword IAmAPassworddd??O??J(Oc|f?X?? b???=??}+)?f"
If you read in details of the article I mentioned before, you would come up with the 'Cost Of ViewState' where it is clearly and beautifully written :
Try it yourself. Download Sample
它是由 ASP.NET 生成的隐藏字段,包含有关页面上所有控件的信息。理想情况下,视图状态不需要加密,因为它不应该包含敏感信息。要指示应加密视图状态,请将
machine.config
文件中的
元素的验证属性设置为3DES
。 MSDN 上有一篇描述 ViewState 的好文章。It is a hidden field generated by ASP.NET that contains information about all the controls on the page. Ideally the view state should not need to be encrypted, as it should never contain sensitive information. To indicate that the view state should be encrypted, set the
<machineKey>
element's validation attribute in themachine.config
file to3DES
. There's a nice article on MSDN describing ViewState.默认情况下,ViewState 不加密,使用 base64 编码。如果您的页面具有带控件的操作,您可能需要使用视图状态。
ViewState's not encrypted as default, using base64 encoding. You may want to use viewstate if your page has an action with controls.
ViewState 是 ASP.NET 用于启用回发模型的一项技术。所有标记为
runat="server"
的控件的状态都存储在此 base64 字符串中。这篇pluralsite 文章详细解释了深度
ViewState is one technique asp.net uses to enable the postback model. The state for all controls that are marked
runat="server"
is stored in this base64 string.This pluralsite article explains in more depth