Viewstate Problem with Multipart/Form Data Post Request

0

I am trying to submit a text value to a form with multipart/form data post request and I am receiving the viewstate value from my get method. But when I send this post request, server returns me that error message;

"ViewStateException: Invalid viewstate. [HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."

this is my main code;

            string viewState;
        string viewStateGenerator;
        string eventValidation;
        string source;
        var boundary = DateTime.Now.Ticks.ToString("x");
        var boundary2 = "----WebKitFormBoundary" + boundary;
        MultipartFormDataContent content = new MultipartFormDataContent(boundary2);            

        string source2 = await HttpMethods.Get(url, referer, cookies);
        viewState = HtmlParser.FindHtmlValue(source2, "//*[@id=\"__VIEWSTATE\"]");
        viewStateGenerator = HtmlParser.FindHtmlValue(source2, "//*[@id='__VIEWSTATEGENERATOR']");

        Multipart.MultipartCreator(content, viewState, "\"__VIEWSTATE\"");
        Multipart.MultipartCreator(content, viewStateGenerator, "\"__VIEWSTATEGENERATOR\"");
        Multipart.MultipartCreator(content, "1", "\"ctl00$hIlk\"");
        Multipart.MultipartCreator(content, "on", "\"ctl00$ContentPlaceHolder1$grdSurecKayit$ctl02$cbSec\"");
        Multipart.MultipartCreator(content, "1", "\"ctl00$ContentPlaceHolder1$grdSurecKayit$ctl02$ddlAboneTipi\"");
        Multipart.MultipartCreator(content, "2019", "\"ctl00$ContentPlaceHolder1$grdSurecKayit$ctl02$txtEsasYil\"");
        Multipart.MultipartCreator(content, "2023", "\"ctl00$ContentPlaceHolder1$grdSurecKayit$ctl02$txtEsasNo\"");
        Multipart.MultipartCreator(content, "55", "\"ctl00$ContentPlaceHolder1$ddlMerci\"");
        Multipart.MultipartCreator(content, "Save", "\"ctl00$ContentPlaceHolder1$btnSurecKaydet\"");
        Multipart.MultipartCreator(content, "0", "\"ctl00$ContentPlaceHolder1$ddlSuresiGectiMiAra\"");
        Multipart.MultipartCreator(content, "-1", "\"ctl00$ContentPlaceHolder1$ddlListe\"");

        source =await HttpMethods.PostMultiPartForm(url2, referer2, cookies, content);

this is my HtmlParse method to get viewstate value from last get request;

public static string FindHtmlValue(string source, string xPath)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(source);
        HtmlNode item = doc.DocumentNode.SelectSingleNode(xPath);
        string itemValue = item.Attributes["value"].Value;
        return itemValue;
    }

and this is my post method code;

public static async Task<string> PostMultiPartForm(string url, string referer, CookieContainer cookies, MultipartFormDataContent content)
    {

        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;
        handler.AllowAutoRedirect = true;
        HttpClient client = new HttpClient(handler);
        client.DefaultRequestHeaders.Add("Referer", referer);
        HttpResponseMessage response = await client.PostAsync(url, content);

        return (await response.Content.ReadAsStringAsync());
    }

This is my MultipartFormCreator Method;

       public static void MultipartCreator(MultipartFormDataContent content,  string value, string name)
    {
        StringContent stringContent = new StringContent(value);
        content.Add(stringContent, name);

    }

My request's form body looks like this after running this code;

------WebKitFormBoundary8d7f3cb5ae5991f
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="__VIEWSTATE"

5ufuqAkp7eQEvA7KmBskVpNcr83vCztlMS1FjElsdR/aCkzJtK/CjbfxchWNyDCDsWG3wL4SZZZ99EfDZD/LQpHLtJEIYUOWWFS8xiiqXqFnR6jQFuesRcjsmi2O/Nf9

And this is the post request body data which is taken from the fiddler(i try to do this process manually and track traffic from fiddler);

------WebKitFormBoundaryKXT6pYoAevqq9UmT
Content-Disposition: form-data; name="__VIEWSTATE"

r63HuFpdyiTcPy3UaarCCjLNr/0vxKDKA9Kn5VynuoXBooAxDIc9onEtJwDTEH3ASQjeVL9oMb9+KZHOjZ15xY4mTsSaFYObgEGaFejIWsKpc+y0s4uG1UwQDD8OirXk

As you can see, request body taken from fiddler is slightly different from the one taken from my code processed. My body form data includes a content-type text part which fiddler body doesnt have. So is that my problem ? Must i remove that content-type text part from my request to dont take invalid viewstate error ?

Thx for answers. Regards.

javascript
c#
html
http
multipartform-data
asked on Stack Overflow May 9, 2020 by sagopaj

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0