PHP CURL-Login auf ASP basierende Website mit PHP/Session/Cookie

M

mw197

Gast
Hi Leute,

folgendes Vorhaben: Ich habe mehrere Server (IIS) im Netz auf denen sich mehrere Benutzer mit immer den gleichen Zugangsdaten anmelden.

Damit dieser ständige Login entfällt, habe ich eine Weboberfläche programmiert, auf der man sich EINMAL anmeldet und man dann eine Liste mit den verfügbaren Servern hat. Wählt man nun einen Server aus, soll der Login automatisch erfolgen.
Die Server, auf denen der Auto-Login ausgeführt werden soll, läuft mit ASP und der Login ist mit dem IIS erstellt via (Standardauthentifizierung).
Unbenannt.PNG
Die Weboberfläche kommt per Rolle der Druck- und Dokumentendienste mit uns lässt sich nachher per https://localhost/printers aufrufen. Da halt viele Server in Betrieb sind, loggt man sich ständig auf allen Seiten ein, was ich gerne ändern möchte damit.


Per CURL kann ich die Website sogar aufrufen und in meiner PHP-Oberfläche ausgeben. Dafür nutze ich folgenden Code.
Rufe ich die Website dann aber mit dem Link im Browser auf, fragt er trotzdem nach den Zugangsdaten...
Er erstellt dem Benutzer anscheinend keine Session.
PHP:
<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://172.16.0.54/printers/'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_USERPWD, 'administrator:geheim'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); //geht ohne diese Zeile genau so gut 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); //geht ohne diese Zeile genau so gut 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; //zeigt mir die Website im eingeloggten Zustand an (Login klappt also, nur der Aufruf dann mit der direkten URL nicht, da wir User wieder abgefragt)

Jemand eine Idee?
 
Und den Cookie bzw. die Session ID gibst du wie an den Browser weiter?
 
Das hatte ich wie folgt schon versucht, funktioniert aber nicht.

PHP:
$username='contoso\administrator';
$password='geheim';
$URL='https://172.16.0.54/printers/';



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password.'&submit=');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
#echo 'Fehler [1]:'.curl_error($ch);
#echo curl_exec($ch);
curl_close($ch);

$cookie = file_get_contents('cookie.txt');
$cookie = explode('ASPSESSIONID', $cookie);
$cookie = explode('	', $cookie[1]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
#curl_setopt($ch, CURLOPT_COOKIE, 'ASPSESSIONID'.$cookie[0].'='.$cookie[1]);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password.'&submit=');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

setcookie('ASPSESSIONID'.$cookie[0], $cookie[1], time()+3600, '/');
echo '<hr />';
echo 'Fehler [2]:'.curl_error($ch);
echo curl_exec ($ch);
curl_close ($ch);
Ergänzung ()

Ausgeben lassen kann ich mir die gesamte Seite auch, also scheint der Login zu klappen.
Als Header kriege ich folgendes, ganz oben im Screenshot zu sehen, angezeigt. Und beim Betreten der Seite direkt per URL fragt er dann wieder nach Logindaten...

Unbenannt.PNG
Ergänzung ()

Ich habe das Script soweit reduziert, dass nun ein Cookie angelegt wird. Logge ich mich auf der Website nun manuell ein, nutzt er sogar das Cookie, was ich per folgendem Script gesetzt habe. Wieso muss ich mich jetzt noch authentifizieren?

PHP:
$username = 'administrator';
$password = 'geheim';
$url = 'https://172.16.0.7/printers/';


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
$result = curl_exec($ch);

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $cookiestring);

parse_str($cookiestring[1], $cookies);

foreach($cookies as $cookiename => $cookievalue)
{
	setcookie($cookiename, $cookievalue, 0);
}
 
Zurück
Oben