"cryptographicexception: padding is invalid and cannot be removed and validation of viewstate mac failed" Code Answer


first of all lets start from the fact, that this error of view state happens on postback.

also i must say that i have done all the things that every one suggest to do to avoid this problem. and i have single machine, but 2 pools that run the same pages.

so someone do an action, ether a man, ether some other search machine by 'clicking' on your pages, or some hacker try to check your system for problems...

i have similar problems (rare but existing ones), and i finally found that people try to hack-test my pages. (from the same ip i have and dos attacks)

i modify the function loadpagestatefrompersistencemedium() that translate the viewstate, and see by logging what exactly was the input, and from what ips... then i started monitor these results and see that the view state was changed by hand - or was totally empty.

on error i just redirect him to the same page...

here is what i did...

public abstract class basepage : system.web.ui.page
    protected override object loadpagestatefrompersistencemedium()
            .. return the base, or make here your decompress, or what ever...
            return base.loadpagestatefrompersistencemedium();            
        catch (exception x)
            string vsstring = request.form[__viewstate];
            string cthepage = request.rawurl;

            ...log the x.tostring() error...
            ...log the vsstring...
            ...log the ip coming from...
            ...log the cthepage...

        // check by your self for local errors
            debug.fail("fail to load view state ! reason:" + x.tostring());

        // if reach here, then have fail, so i reload the page - maybe here you
        // can place somthing like ?rnd=randomnumber&errorid=1 and show a message
        responce.redirect(request.rawurl, true);        

        // the return is not used after the redirect
        return string.empty;

second reason

now there is one more reason why this can happen, and the reason is because some one click on your page before the __eventvalidation is loaded.

this eventvalidation is placed on the last button-even that asp.net found, and if you have some of them on many place on the page, or near the button, then this go to the end of the page.

so even if you see the viewstate on the top of the page, where is the validation ??? maybe this never loaded - page corrupt ?, too fast user click on page ?

<input type="hidden" name="__eventvalidation" id="__eventvalidation" ... >

to avoid this kind of problem i made a simple javascript that i do not let it press the button unless this input have been loaded !!!.

one more comment, the __eventvalidation is not always presents ! so is maybe safer not to search for this field if you make a general solution, but to make a javascript trick to just check if the full page is loaded, or something else that you think.

here is my final solution with jquery: (note that i check on pageload if eventvalidation exist !). i have this placed on my masterpages.

<script language="javascript" type="text/javascript">
    function allowformtorun()
        var myeventvalidation = $("#__eventvalidation");

        if(myeventvalidation.length == 0 || myeventvalidation.val() == ""){
            alert("please wait for the page to fully loaded.");
            return false;

        return true; 

protected void page_load(object sender, eventargs e)
    // i do not know if page can be null - just in case i place it.
    if (page != null && page.enableeventvalidation)
        form.attributes["onsubmit"] = "return allowformtorun();";

you can test by placing near the button of your page a delay.

<% system.threading.thread.sleep(5000); %>


today i see in log this message again for webresource and what i discover is that a bot getting the pages and make all the character on the links in lower case, including the parameters, so this was one more reason to not getting the correct encoded string, and throw a message like padding is invalid and cannot be removed.

hope this help you more.

By lmboom on July 1 2022

Answers related to “cryptographicexception: padding is invalid and cannot be removed and validation of viewstate mac failed”

Only authorized users can answer the Search term. Please sign in first, or register a free account.