简体   繁体   中英

Validation of viewstate MAC failed. - Not on a web farm, happens on a button click

I've seen this question asked a lot but none of the questions or answers I've seen seem to address my problem. As the title suggests, I am getting the always fun Validation of Viewstate error.

My company's website is hosted on Amazon EC2 so to the best of my knowledge is not a web farm as this is the only page the problem crops up.

We have a simple reviews page which comes up great and all elements on the page work great, including leaving new reviews. However the "flag review" button generates the viewstate error when clicked.

The actual code that is run when the button is clicked is as follows:

protected void btnFlagReview_Click(object sender, EventArgs e)
{
    try
    {
        LinkButton btn = (LinkButton)sender;
        int id = Int32.Parse(btn.Attributes["rid"]);
        testimonials.updateModerated(false, id);

        MailMessage mail = new MailMessage();
        mail.To.Add("oursupport@emailaddress.com");
        mail.From = new MailAddress("ourwebmaster@emailaddress.com");
        mail.Subject = "A review has been flagged for moderation";
        string Body =
            "A user review has been flagged for moderation. Please Approve/Delete this review in the Admin Panel. ";
        mail.Body = Body;
        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Send(mail);
        }
        catch (SmtpException ex)
        {
            if (ex.StatusCode == SmtpStatusCode.InsufficientStorage)
                smtp.Send(mail);//Send again to ensure this email gets sent 
        }

        string script = "<SCRIPT type='text/javascript'>alert('This review has been flagged for moderation.')</SCRIPT>";
        Type t = typeof(String);
        ClientScript.RegisterStartupScript(t, "Script", script);
    }
    catch(Exception ee) {
        businessName.Text = ee.ToString();
    }
}

The code from the actual button is as follows:

<asp:LinkButton ID="btnFlagReview" OnClick="btnFlagReview_Click" rid='<%# Eval("testimonialID") %>' runat="server">flag</asp:LinkButton>

I have even tried the solution I've found in other questions across the web and here like changing the page's validateRequest, enableEventValidation, and viewStateEncryptionMode just to see if they would work.

I am at a loss, considering the flag review action is so simple in comparison to a lot of the other things we do. Any help would be greatly appreciated.

I had a similar problem once, and it ended up being because I had an action attribute on my form tag. Check to see if you have one, and if so, remove it.

I asked in my comment which version of the framework you are using. The reason being that there were a number of patches done to mitigate this issue.

See http://blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx

A lot of these seem to boil down to someone clicking a button or link on a page that isn't completely rendered back to the browser. Which seems likely in your situation for a link titled "flag"...

The first thing to try is to have all of the windows updates applied. After that you might have to do something like place the machine key in the web.config file or create a page base class which emits the relevant hidden fields at the top of the form instead of at the end.

===== UPDATE based on comments to question =====

See: http://support.microsoft.com/kb/316920/

The server.transfer itself may be causing the issue. Review that KB for info to see if it applies and how to get around it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM