The security validation for this page is invalid. Click Back in your Web

The security validation for this page is invalid. Click Back in your Web

Got this error when I turned on Annoymous Access in MOSS.

Navigate thru the followwing and turn the security validation Off
Central Administration--->application management---->web application settings-->"turn security validation off"

This works but is bad practice
Instead in order to fix my problem I used the following code:

SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate()
{
using (SPSite oSite = new SPSite(https://whatever/))
{
using (SPWeb web = oSite.OpenWeb())
{
SPWebApplication webApp = web.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
web.AllowUnsafeUpdates = true;

.......Code goes here

web.AllowUnsafeUpdates = false;
webApp.FormDigestSettings.Enabled = true;
}
}
}));

You might not need all to disable the FormDigestSettings or AllowUnsafeUpdates but one of them works.