In the world of web applications, managing the client-side state effectively is crucial for both performance and security. ASP.NET View State is a common mechanism used to maintain this state across various page requests, but it can introduce significant challenges. Especially, when deploying applications on platforms like Microsoft Azure. In this blog, we will discuss these issues and introduce a powerful solution, i.e., using NCache for Azure to offload View State storage to a distributed cache.
1 2 3 4 5 6 7 8 9 |
<input id="__VIEWSTATE" type="hidden" name="__VIEWSTATE" value="wEPDwUJNzg0MDMxMDA1D2QWAmYPZBYCZg9kFgQ CQ9kFgICBQ9kFgJmD2QWAgIBDxYCHhNQcm2aW91c0NvbnRyb2xNb2RlCymIAU1pY3 Jvc29mdC5TaGFyZVBvaW50LldlYkNvbnRyb2xzLlNQQ29udHJbE1vZGUsIE1pY3Jv 29mdC5TaGFyZVBvaW50LCBWZXJzaW9uPTEyLjAuMC4wLCBDdWx0dXJlPW5ldXRyWw sIFB1YmxpY0tleVRva2VuPTcxZTliY2UxMTFlOTQyOWMBZAIDD2QWDgIBD2QWBgUm Z19lMzI3YTQwMF83ZDA1XzRlMjJfODM3Y19kOWQ1ZTc2YmY1M2IPD2RkZAUmZ18yN DQ3NmI4YV8xY2FlXzRmYTVfOTkxNl8xYjIyZGYwNmMzZTQPZBYCZg8PZBYCHgVjbG DQWBgUmZ19lMzI3YTQwMF83ZDA1XzRlMjJfODM3Y19kOWQ1ZTc2YmY1M2IPD2...."/> |
Problems with ASP.NET View State in Microsoft Azure
Unfortunately, the ASP.NET View State can become significantly large, particularly in Azure ASP.NET applications with numerous complex controls. This large View State payload increases the amount of data transmitted between the browser and server, leading to degraded application performance and increased bandwidth usage, often reaching hundreds of kilobytes per request. Furthermore, including confidential data in the View State exposes it to interception and manipulation by attackers.
Solution to These Problems
NCache for Azure is an extremely fast and scalable distributed cache. It allows you to store the actual ASP.NET View State in a distributed cache on the server side and send a token as view state to the client in a request payload. This reduces the request payload size. The server uses this view state token to locate the correct ASP.NET View State in NCache. A smaller payload resolves performance and bandwidth utilization issues. Moreover, the server stores this view state in NCache, ensuring it remains hidden from clients and mitigating security concerns.
Here is an example of using a token instead of ASP.NET View State with NCache for the Azure ASP.NET View State provider:
1 2 |
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="cf8c8d3927ad4c1a84dsadfgsdsfdsda7f891bb89185" /> |
Using NCache for Azure ASP.NET View State Caching
Follow the steps mentioned below to use Azure View State Caching with NCache.
Step 1: Create an app.browser file in App_browsers directory. Plug in page adapters in the app.browser file as follows:
1 2 3 4 5 6 |
<browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.Page" adapterType="Alachisoft.NCache.Adapters.PageAdapter" /> </controlAdapters> </browser> |
Step 2: After that, add the following assembly reference in compilation section of web.config file.
1 2 3 4 5 6 |
<compilation defaultLanguage="c#" debug="true" targetFramework="4.0"> <assemblies> <add assembly="Alachisoft.NCache.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=CFF5926ED6A53769"/> </assemblies> </compilation> |
Step 3: Next, register NCache for Azure config section in your web.config file.
1 2 3 4 5 6 7 |
<configSections> <sectionGroup name="ncContentOptimization"> <section name="settings" type="Alachisoft.NCache.ContentOptimization.Configurations.ContentSettings" allowLocation="true" allowDefinition="Everywhere"/> </sectionGroup> </configSections> |
Step 4: Then, specify settings for your config section in web.config file (that was registered above). These settings control NCache for Azure ASP.NET View State Caching features.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<ncContentOptimization> <settings webAppId="ViewStateApp" enableViewstateCaching="true" groupedViewStateWithSessions="true" maxViewStatePerSession="5" viewstateThreshold="1" cacheUriMapping="true" enableTrace="false" enableMetadataLocking="true" enableMinification="true" releaseLockInterval="5"> <cacheSettings cacheName="demoCache" connectionRetryInterval="300"> <expiration type="Absolute" duration="1" /> </cacheSettings> </settings> </ncContentOptimization> |
Conclusion
NCache for Azure offers a seamless solution for storing ASP.NET View State on the server side in a distributed cache. This eliminates the need for code changes in your Azure applications. This approach optimizes performance by minimizing request payload and bandwidth usage. Moreover, it enhances security by keeping sensitive data off the client side.
You’re right about NCache sending the original string to ASP.NET.
As far as view state not caching, there must be something wrong with your configuration specific to your environment. We will shortly send an email to you at your registered email address to coordinate and further help you out.
Hi
I have configured all steps that you described in the article, but, the view state string didnt change and didnt get smaller size, the main idea is ncache replace the string with his own string and then, ncache in the execution time will pass the original string to asp.net, i’m wrong?
please let me know, how can i get more information or if will be posible to get remote assistance ?
I’m glad you thought of it so.
By Default, NCache keeps all ViewStates indefinitely in the cache regardless how late the post back is issued. Therefore all view state requests will be handled by the cache.
On the other hand, you can also associate time base expirations (Absolute or sliding) to ViewState and set the value high enough to avoid cache misses even after a long pots back time.
You can also group ViewState with NCache Sessions. This way when a user session is removed from cache, all related ViewStates are automatically removed. This helps to avoid extra memory load on the cache providing you the best performance.
NCache does sound quite useful.
In case of viewstate in page, the page can be posted back even after a very long time without any impact.
How does NCache handle this scenario?