Securing View State in ASP.NET

Securing View State in ASP.NET

View state data is stored in the web page in a hidden field and is encoded using base64 encoding. In addition, a hash of the view state data is created from the data by using a machine authentication code (MAC) key. The hash value is added to the encoded view state data and the resulting string is stored in the page. When the page is posted back to the server, the ASP.NET page framework re-computes the hash value and compares it with the value stored in view state. If the hash values do not match, an exception is raised that indicates that view state data might be invalid.

By creating a hash value, the ASP.NET page framework can test whether the view state data has been corrupted or tampered with. However, even if it is not tampered with, view state data can still be intercepted and read by malicious users.

Using the MAC for Computing the View State Hash Value

The MAC key that is used to compute the view state hash value is either auto-generated or specified in the Machine.config file. If the key is auto-generated, it is created based on the MAC address of the computer, which is the unique GUID value of the network adapter in that computer.

It can be difficult for malicious users to reverse-engineer the MAC key based on the hash value in view state. Thus, MAC encoding is a reasonably reliable way to determine whether view state data has been changed.

In general, the larger the MAC key that is used to generate the hash, the less likely it is that the hash value for different strings will be the same. When the key is auto-generated, ASP.NET uses SHA-1 encoding to create a large key. However, in a Web-farm environment, the key must be the same across all the servers. If the key is not the same, and the page is posted back to a different server than the one that created the page, the ASP.NET page framework will raise an exception. Therefore, in a Web farm environment, you should specify a key in the Machine.config file instead of letting ASP.NET auto-generate one. In that case, make sure that you create a key that is long enough to offer sufficient security for the hashed value. However, the longer the key is, the more time it takes to create a hash. Therefore, you must weigh security needs versus performance needs.

Encrypting View State

Although MAC encoding helps prevent tampering with view state data, it does not prevent users from viewing the data. You can prevent people from viewing this data in two ways: by transmitting the page over SSL, and by encrypting the view state data. Requiring the page to be sent over SSL can help prevent data-packet sniffing and unauthorized data access by people who are not the intended recipients of the page.

However, the user who requested the page can still view the view state data because SSL decrypts the page to display it in the browser. This is fine if you are not concerned about authorized users having access to view state data. However, in some cases, controls might use view state to store information that no users should have access to. For example, the page might contain a data-bound control that stores item identifiers (data keys) in view state. If those identifiers contain sensitive data, such as customer IDs, you should encrypt the view state data in addition to or instead of sending the page over SSL.

To encrypt the data, set the page's ViewStateEncryptionMode property to true. If you store information in view state, you can use regular read and write techniques; the page handles all encryption and decryption for you. Encrypting view state data can affect the performance of your application. Therefore, do not use encryption unless you need it.

No comments:

Post a Comment

Labels

.NET Framework Interview Questions (7) .NET Interview Questions (10) .NET Remoting Interview Questions (1) ADO.NET and BLOB Error (1) ADO.NET Interview Questions (4) Agile Articles (9) AJAX Articles (5) AJAX Interview Questions (11) Algorithms (2) Analytics Articles (2) Analytics Interview Questions (3) Android FAQs - Part 1 (2) Articles (13) ASP.NET Articles (24) ASP.NET Error and Resolution (4) ASP.NET Interview Questions (23) ASP.NET Tutorial (8) AWS Interview Questions (16) Business Analyst Interview Questions (1) Cloud Computing Interview Questions (16) CSharp Articles (17) CSharp Interview Questions (32) CSharp Tutorial (17) Data Analysis (2) Data Structure (1) Design Pattern Articles (5) DevOps Tutorial (1) Digital Marketing Interview Questions (1) Download Templates (1) Error Resolution (6) Excel Articles (9) Excel Macros (1) Excel Tips and Tricks (10) HTML5 Interview Questions (3) HTML5 Tutorial (3) Interview Preparation (2) Interview Questions (24) Introduction to Business Analytics (10) Introduction to Python (7) Introduction to R Programming (23) JAVA Articles (6) Java Tutorial (5) LINQ Articles (4) LINQ Interview Questions (2) LINQ Tutorial (3) Microservices Interview Questions (1) MVCInterviewQuestions (2) OOPs Interview Questions (4) Oracle 9i Tutorial (14) Oracle Articles (2) Oracle Interview Questions (15) Outlook Error (1) PHP Interview Questions (3) PHP Tutorial (3) Product Management (12) Product Management Interview Questions (14) Product Owner Interview Questions (2) Program Management (5) Project Management (13) Project Management Articles (34) Project Management Interview Questions (25) Quiz (1) RallyDev Help (1) Scrum Master Interview Questions (11) Selenium Tutorial (1) Sharepoint Articles (1) SQL Interview Questions (23) SQL Server Articles (20) SSIS Interview Questions (6) SSRS Interview Questions (1) Technical Program Management (12) Technical Program Management - Interview Questions (24) TechnicalProgramManagement (5) Threading Interview Questions (2) Tutorial (8) UML Articles (3) UML Interview Questions (2) Unix (3) UNIX Tutorial (3) WCF Articles (20) WCF Interview Questions (9) WCF Quiz (2) WCF Tutorial (16) Web Service Articles (5) Web Service Interview Questions (3) Window Azure (1) XML Articles (6) XML Interview Questions (3) XML Tutorial (3)