Validation of Viewstate MAC Failed 

Posted by on Oct 30, 2008 in ASP.NET, Resources, Small Business Web Design | 4 comments

The Validation of Viewstate MAC failed error commonly occurs when an ASP.NET application developer uses the Membership, Roles and/or Profile Providers included with the .NET Framework. The usual scenario is the developer creates the application on their local machine and stores the project files in a local directory. When the developer copies or publishes the project files to another local directory or to a remote server, the login system doesn’t appear to function properly.

The solution is to manually add the applicationName attribute to the <providers> node of your web.config file and give it a value. By default ASP.NET auto generates the applicationName using your project’s root directory path if the applicationName attribute isn’t declared in your web.config. The applicationName is important because your ASP.NET Application Service database uses it in a number of tables. The applicationName is a relatively long string of characters and will look similar to: cea2acdb-97b7-4b58-ad12-22efg3bd582q.

If you move your project to any other directory either local or remote, a new applicationName string will be auto generated and added to your ASP.NET Application Service database. Any new users, roles, or profiles you create will be tied to the new applicationName. Once you move your application to a new directory, any users, roles, or profile information you created while your project resided in your previous directory will not function or throw the following error: “Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. Auto Generate cannot be used in a cluster.” Each time you move your project to a different directory you’ll run into this problem.

To solve this, first go to the Server Explorer in Visual Studio and open the database containing your ASP.NET Application Service database. Right click on the table named aspnet_Applications (dbo) and select “Show Table Data”. Look to see what your ApplicationName and LoweredApplicationName fields are named. It should be “/”. If it’s named anything other than “/”, rename both fields “/” as shown below.

Validation Viewstate MAC Failed

Next open your web.config file. Add the applicationName attribute to your provider declaration(s). You should set the applicationName attribute equal to “/” to match the “/” fields in your aspnet_Applications (dbo) table. An example is provided below:

<roleManager enabled=”true” defaultProvider=”CustomizedRoleProvider”>
<providers>
<add name=”CustomizedRoleProvider”
type=”System.Web.Security.SqlRoleProvider”
connectionStringName=”MyConnectionString”
applicationName=”/”
/>
</providers>
</roleManager>

<membership defaultProvider=”CustomizedMembershipProvider”>
<providers>
<add name=”CustomizedMembershipProvider”
type=”System.Web.Security.SqlMembershipProvider”
connectionStringName=”MyConnectionString”
applicationName=”/”
minRequiredPasswordLength=”5″
minRequiredNonalphanumericCharacters=”0″
/>
</providers>
</membership>

Make sure you add the applicationName attribute to all your users, roles, profile, and/or any other <providers> nodes in your web.config or you’ll run into problems. In the example above I’m using both the Role and Membership providers so I added it to both declarations.
Once you declare the applicationName attribute, ASP.NET will always use that application name when writing and connecting to the ASP.NET application service database regardless of where your application resides.

Steve Kozyk
CEO/Founder ITegrity
skozyk[at]itegritygroup.com
www.itegritygroup.com

4 Responses to “Validation of Viewstate MAC Failed”

  1. Can you please mention the solution for mysql database.

    • Raju,

      First it is assumed that you are using MySQL with ASP.NET and Visual Studio. You can certainly do this, but you may want to consider using MS SQL Express, a free version of MS SQL from Microsoft. It has the full feature suite of MS SQL, except it imposes a 4 gig database size restriction, a limit you’ll probably never reach unless you have a very large database or you are storing serialized data such as images. MS SQL and Express are recommended to use with a .NET services architecture over MySQL. Here’s a quick article on MS SQL vs. MySQL.

      If you do choose to use MySQL, the solution is very similar. View your MySQL database with a database management program such as phpMyAdmin and follow the instructions in my article. Other than this, the solution should be identical.

  2. I have a very simple one page (Contact Us) web application. I do not use use Membership, Roles, or Profile Providers. I get the “Validation of Viewstate MAC failed” exception when the user submits the form.

    Tools: VS 2008, ASP.NET 3.5
    I use validation controls on the form.
    Works on my development system.
    I get the exception when running the form on the client’s web site which is not hosted in a “web farm” nor “cluster”.

    Any ideas?

Leave a Reply

*