什么是视图状态?它是如何编码的?是否加密?谁使用 ViewState?

发布于 2024-08-22 18:04:04 字数 42 浏览 7 评论 0原文

什么是视图状态?它是如何编码的?是否加密?谁使用 ViewState?

What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡忘如思 2024-08-29 18:04:04

视图状态是一种哈希映射(或者至少您可以这样认为),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 encrypted encoded 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.

蔚蓝源自深海 2024-08-29 18:04:04

如果您确实想理解 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.

初相遇 2024-08-29 18:04:04

请允许我与大家分享我今天学到的东西。

什么是视图状态?

Microsoft® ASP.NET 视图状态,简而言之,是使用的技术
用于保存对 Web 窗体状态的更改的 ASP.NET 网页
跨回发。

视图状态将页面控件的值存储为字符串
采用某些散列和编码技术进行散列和编码。它仅
包含有关页面及其控件的信息

如果我有这样的内容:

protected void Page_Load(object sender, EventArgs e)
{
    ViewState["UserName"] = "Shubh Dasgupta";
    ViewState["Password"] = "IAmAPassword";
}

默认情况下,页面的视图状态放置在名为 __VIEWSTATE 的隐藏表单字段中。

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
     value="/wEPDwULLTE2MTY2ODcyMjkPFgQeCFVzZXJOYW1lBQ5TaHViaCBEYXNndXB0YR4IUGFzc3dvcmQFDElBbUFQYXNzd29yZGRk2/xP37hKKE9jfGYYzFjLuwpi6rHlPdXhfSspF6YRZiI=" />

了解更多内容

它是如何编码的?是否加密?

默认情况下,ViewState 已编码但未加密。让我们采用前面的输入类型值来运行以下代码:

protected void btnDecode_Click(object sender, EventArgs e)
{
    //txtViewState.Text = "/wEPDwULLTE2MTY2ODcyMjkPFgQeCFVzZXJOYW1lBQ5TaHViaCBEYXNndXB0YR4IUGFzc3dvcmQFDElBbUFQYXNzd29yZGRk2/xP37hKKE9jfGYYzFjLuwpi6rHlPdXhfSspF6YRZiI="
    string str = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(txtViewState.Text));
    lblDecodedString.Text = str;
}

上述代码的输出将是 ? -1616687229 UserName Shubh DasguptaPassword IAmAPassworddd??O??J(Oc|f?X?? b???=??}+)?f"

如果您详细阅读了我之前提到的文章,您会发现会提出“Cost Of ViewState”,它写得清晰漂亮:

在所有页面访问中,在保存视图状态阶段 Page 类
收集其所有控件的集体视图状态
控制层次结构并将状态序列化为 Base-64 编码
细绳。 (这是在隐藏的 __VIEWSTATE 中发出的字符串
表单提交。)类似地,在回发时,加载视图状态阶段需要
反序列化持久视图状态数据,并更新相关
控件层次结构中的控件。

自己尝试一下。 下载示例

Allow me to share with you what I learned today.

What is ViewState?

Microsoft® ASP.NET view state, in a nutshell, is the technique used by
an ASP.NET Web page to persist changes to the state of a Web Form
across postbacks.

View State stores the value of page controls as a string which is
hashed and encoded in some hashing and encoding technology. It only
contain information about page and its controls

If I have something like this:

protected void Page_Load(object sender, EventArgs e)
{
    ViewState["UserName"] = "Shubh Dasgupta";
    ViewState["Password"] = "IAmAPassword";
}

The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
     value="/wEPDwULLTE2MTY2ODcyMjkPFgQeCFVzZXJOYW1lBQ5TaHViaCBEYXNndXB0YR4IUGFzc3dvcmQFDElBbUFQYXNzd29yZGRk2/xP37hKKE9jfGYYzFjLuwpi6rHlPdXhfSspF6YRZiI=" />

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:

protected void btnDecode_Click(object sender, EventArgs e)
{
    //txtViewState.Text = "/wEPDwULLTE2MTY2ODcyMjkPFgQeCFVzZXJOYW1lBQ5TaHViaCBEYXNndXB0YR4IUGFzc3dvcmQFDElBbUFQYXNzd29yZGRk2/xP37hKKE9jfGYYzFjLuwpi6rHlPdXhfSspF6YRZiI="
    string str = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(txtViewState.Text));
    lblDecodedString.Text = str;
}

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 :

On all page visits, during the save view state stage the Page class
gathers the collective view state for all of the controls in its
control hierarchy and serializes the state to a base-64 encoded
string. (This is the string that is emitted in the hidden __VIEWSTATE
form filed.) Similarly, on postbacks, the load view state stage needs
to deserialize the persisted view state data, and update the pertinent
controls in the control hierarchy.

Try it yourself. Download Sample

世态炎凉 2024-08-29 18:04:04

它是由 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 the machine.config file to 3DES. There's a nice article on MSDN describing ViewState.

人生百味 2024-08-29 18:04:04

默认情况下,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.

他是夢罘是命 2024-08-29 18:04:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文