Viewstate timeout error

不羁岁月 提交于 2019-12-11 22:07:33

问题


I develop mostly for desktop so, I tend to think as WebForms as a web equivalent of WinForms. Unfortunetly this is not true.

Recently I have discovered that the Viewstate have some kind of timeout.

My problem is similar as I have read in most questions, in particular here (in my case is only around 5 to 10 minutes).

Here Microsoft says that one solution for this problem is:

 <asp:Page EnableViewStateMac="False" />

However as we can read further they say:

Security Note:
This attribute should never be set to false in a production Web site, 
even if the application or page does not use view state. 
The view state MAC helps ensure the security of other ASP.NET functions 
in addition to view state.

For this reason I don't want to set EnableViewStateMac to false and I have no access to my server (is shared hosting).

My question is: can we store the Viewstate between postbacks even if our page stay idle for a long time? If yes, how?

Thank you


回答1:


The viewstate is encrypted using a machine key to ensure that it is not tampered with during postback. The machine key used to encrypt the viewstate is by default auto-generated and if the time out happens then the key's decryption will fail because the machinekey will get regenerated.

The machinekey is by default available at machine level config file.

<machineKey validationKey="AutoGenerate,IsolateApps"  
            decryptionKey="AutoGenerate,IsolateApps" 
            validation="SHA1" decryption="Auto" />

To fix this, you can use your own defined machine key. You can generate using online tools as well, like this or through IIS.

How to add this machinekey to web.config can be read at MSDN.

It should be placed under the system.web section, like this -

<configuration>
  <system.web>
    <machineKey decryptionKey="Decryption key goes here,IsolateApps" 
                validationKey="Validation key goes here,IsolateApps" />
  </system.web>
</configuration>


来源:https://stackoverflow.com/questions/21003108/viewstate-timeout-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!