开发者

Logging-in into a ASP application via PHP (Cookie/POST value issues?)

I need to login into a certain website before I can attempt a data download. I am trying to do that via a PHP script.

The login page is located at:

http://abc.example.com/login.aspx

Which has a form like this (extraneous elements removed):

<form name="Form1" method="post" action="login.aspx" id="Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="dDw1MTU3NTkxNTI7O2w8Y2hrYm94Oz4+08TlRVm+gb75yz3dIctChP3qf/E=" />

<script language="javascript" type="text/javascript">
<!--
    function __POSTBack(eventTarget, eventArgument) {
        var thisform;
        if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
            thisform = document.Form1;
        }
        else {
            thisform = document.forms["Form1"];
        }
        thisform.eventTarget.value = eventTarget.split("$").join(":");
        thisform.eventArgument.value = eventArgument;
        thisform.submit();
    }
// -->
</script>
<input name="text_userid" id="text_userid" type="text" />
<input name="text_password" type="password" maxlength="30" id="text_password" />
<input name="chkbox" id="chkbox" type="checkbox" value="checkbox" />
<a id="submit" href="javascript:__POSTBack('submit','')"><img src="images/login.jpg"></a>
</form>

I'm trying to login into it via PHP, but I'm not sure what I'm doing wrong. Here's what I'm doing:

<?php
$pages = array(
            'login_pre' =>  'http://abc.example.com/login.aspx',
            'login' =>  'http://abc.example.com/login.aspx');
$ch = curl_init();

//Set options for curl session
$options = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6',
         CURLOPT_HEADER => TRUE,
         CURLOPT_RETURNTRANSFER => TRUE,
         CURLOPT_COOKIEFILE => 'cookie.txt',
         CURLOPT_COOKIEJAR => 'cookies.txt');

$options[CURLOPT_URL] = $pages['login_pre'];
curl_setopt_array($ch, $options);
$login_pre_content = curl_exec($ch);

preg_match('/__VIEWSTATE" value="(.*)"/', $login_pre_content, $matches);
$VIEWSTATE = $matches[1];

//Login
$options[CURLOPT_URL] = $pages['login'];
$options[CURLOPT_POST] = TRUE;

$options[CURLOPT_POSTFIELDS] = '__EVENTTARGET=submit&__EVENTARGUMENT=&__VIEWSTATE='.$VIEWSTATE.'&text_userid=MYEMAIL&text_password=MYPASSWORD&chkbox=on';

$options[CURLOPT_FOLLOWLOCATION] = TRUE;
curl_setopt_array($ch, $options);
$login_post_content = curl_exec($ch);

echo $login_post_content;

//Close curl session
curl_close($ch);
?>

If I do the above, I get the following error message (within the webpage generated by $login_post_content):

Invalid length for a Base-64 char array.

With a stack trace as follows:

[FormatException: Invalid length for a Base-64 char array.]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.LosFormatter.Deserialize(String input) +24
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101

[HttpException (0x80004005): Invalid_Viewstate
    Client IP: MYIPADDRESS
    Port: 24885
    User-Agent: Mozilla/5.0 (Windows; U开发者_Go百科; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
    ViewState: dDw1MTU3NTkxNTI7O2w8Y2hrYm94Oz4 08TlRVm gb75yz3dIctChP3qf/E=
    Http-Referer: 
    Path: /login.aspx.]
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +442
   System.Web.UI.Page.LoadPageViewState() +18
   System.Web.UI.Page.ProcessRequestMain() +441

HOEVER, if I modify the code as follows:

$options[CURLOPT_POSTFIELDS] = '...&__VIEWSTATE='.base64_encode($VIEWSTATE).'...');

I get the following error message:

Unable to validate data.

With a stack trace as follows:

[HttpException (0x80004005): Unable to validate data.]
   System.Web.Configuration.MachineKey.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength) +195
   System.Web.UI.LosFormatter.Deserialize(String input) +59

[HttpException (0x80004005): Authentication of viewstate failed.  1) If this is a cluster, edit <machineKey> configuration so all servers use the same validationKey and validation algorithm.  AutoGenerate cannot be used in a cluster.  2) Viewstate can only be posted back to the same page.  3) The viewstate for this page might be corrupted.]
   System.Web.UI.LosFormatter.Deserialize(String input) +117
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101

[HttpException (0x80004005): Invalid_Viewstate
    Client IP: MYIPADDRESS
    Port: 25029
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
    ViewState: ZER3MU1UVTNOVGt4TlRJN08ydzhZMmhyWW05NE96NCswOFRsUlZtK2diNzV5ejNkSWN0Q2hQM3FmL0U9
    Http-Referer: 
    Path: /login.aspx.]
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +442
   System.Web.UI.Page.LoadPageViewState() +18
   System.Web.UI.Page.ProcessRequestMain() +441


The values of form fields need to be urlencoded. In particular, the value of ViewState needs to be properly urlencoded (it has + and = signs in it, which need to be escaped).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜