Making ViewState More Secure
Unencrypted view state in ASP.NET 2.0 could leak sensitive information
https://www.rapid7.com/db/vulnerabilities/http-asp-dot-net-unencrypted-viewstate
Taking a Bite Out of ASP.NET ViewState
Because it's not formatted as clear text, folks sometimes assume that ViewState is encrypted—it's not. Instead, ViewState is merely base64-encoded to ensure that values are not altered during a roundtrip, regardless of the response/request encoding used by the application.
There are two levels of ViewState security you may wish to add to your application:
- Tamper-proofing
- Encryption
It's important to note that ViewState security has a direct effect on the time required to process and render an ASP.NET page. In short, more secure is slower, so don't add security to ViewState if you don't need it.
Tamper-Proofing 防止篡改
A hashcode will not secure the actual data within the ViewState field, but it will greatly reduce the likelihood of someone tampering with ViewState to try to spoof your application, that is, posting back values that your application would normally prevent a user from inputting.
You can instruct ASP.NET to append a hashcode to the ViewState field by setting the EnableViewStateMAC attribute:
<%@Page EnableViewStateMAC=true %>
EnableViewStateMAC can be set at the page or application level. Upon postback, ASP.NET will generate a hashcode for the ViewState data and compare it to the hashcode store in the posted value. If they don't match, the ViewState data will be discarded and the controls will revert to their original settings.
By default, ASP.NET generates the ViewState hashcode using the SHA1 algorithm. Alternatively, you can select the MD5 algorithm by setting <machineKey> in the machine.config file as follows:
<machineKey validation="MD5" />
Encryption 加密
You can use encryption to protect the actual data values within the ViewState field. First, you must set EnableViewStatMAC="true"
, as above. Then, set the machineKey validation type to 3DES. This instructs ASP.NET to encrypt the ViewState value using the Triple DES(Triple Data Encryption Algorithm三重数据加密算法) symmetric encryption algorithm.
<machineKey validation="3DES" />
ViewState Security on a Web Farm
By default, ASP.NET creates a random validation key and stores it in each server's Local Security Authority (LSA).
In order to validate a ViewState field created on another server, the validationKey for both servers must be set to the same value.
If you secure ViewState by any of the means listed above for an application running in a Web Farm configuration, you will need to provide a single, shared validation key for all of the servers.
The validation key is a string of 20 to 64 random, cryptographically-strong bytes, represented as 40 to 128 hexadecimal characters. Longer is more secure, so a 128-character key is recommended for machines that support it. For example:
<machineKey validation="SHA1" validationKey=" F3690E7A3143C185AB1089616A8B4D81FD55DD7A69EEAA3B32A6AE813ECEECD28DEA66A 23BEE42193729BD48595EBAFE2C2E765BE77E006330BC3B1392D7C73F" />
Summary
ASP.NET ViewState is a new kind of state service that developers can use to track UI state on a per-user basis.
There's nothing magical about it.
It simply takes an old Web programming trick—roundtripping state in a hidden form field—and bakes it right into the page-processing framework.
But the result is pretty wonderful—a lot less code to write and maintain in your Web-based forms.
You won't always need it, but when you do, I think you'll find ViewState is a satisfying addition to the feast of new features ASP.NET offers to page developers.
扩展阅读
Taking a Bite Out of ASP.NET ViewState
Understanding ASP.NET View State
How To: Configure MachineKey in ASP.NET 2.0
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-11-01 DevExpress Crack
2016-11-01 GitHub如何在自己的Stars中进行搜索