Event error for screen scripting

I am trying to clear the login page of the site I'm working on and send the username / password using the login code for the site. I want to do this in the verification service for reasons of website health. I am facing several problems, the first of which concerns receiving this message:

Exception information: Exception type: ArgumentException Exception message: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

I found sites that say I will have to disable eventvalidation, but I do not want to do this for security reasons. Is there any way around this?

Here is the code. I basically took it straight from C. Scott Allen's article here: http://odetocode.com/Articles/162.aspx

  StringBuilder sb = new StringBuilder();
        var encryptedConnectionString = GlobalDataObject.EncryptSecure("conn string here", GlobalDataObject.Seed);
        sb.AppendFormat("client_id={0};", "client");
        sb.AppendFormat("client_directory={0};", "client");
        sb.AppendFormat("user_id={0};", "12");
        sb.AppendFormat("conn_string={0};", encryptedConnectionString);
        StringBuilder cookiesString = sb;

        HttpWebRequest webRequest = WebRequest.Create("http://localhost/site/login.aspx?c=client") as HttpWebRequest;
        webRequest.Headers.Add("Cookie", cookiesString.ToString());
        StreamReader responseReader = new StreamReader(
              webRequest.GetResponse().GetResponseStream()
           );
        string responseData = responseReader.ReadToEnd();
        responseReader.Close();

        // extract the viewstate value and build out POST data
        string viewState = ExtractViewState(responseData);
        string postData = string.Format("__VIEWSTATE={0}&Login1$Password={1}&Login1$UserName={2}&Login1$LoginButton={3}",
                                                            viewState,
                                                  HttpUtility.UrlEncode(username),
                                                  HttpUtility.UrlEncode(password),
                                                  "Log In");

        // have a cookie container ready to receive the forms auth cookie
        CookieContainer cookies = new CookieContainer();

        // now post to the login form
        webRequest = WebRequest.Create("http://localhost/site/login.aspx") as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;

        // write the form values into the request message
        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postData);
        requestWriter.Close();
        webRequest.AuthenticationLevel = AuthenticationLevel.None;
        // we don't need the contents of the response, just the cookie it issues
        webRequest.GetResponse().Close();  ///ERROR HAPPENS HERE

        // now we can send out cookie along with a request for the protected page
        webRequest = WebRequest.Create("http://localhost/site/user/home.aspx") as HttpWebRequest;
        webRequest.CookieContainer = cookies;
        responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

        // and read the response
        responseData = responseReader.ReadToEnd();
        responseReader.Close();

        return responseData;

Thank.

+3
source share
1 answer

, , , . , , HTML - .

: , .

+2

Source: https://habr.com/ru/post/1733074/


All Articles