How to consume a WCF Web Service that uses custom username validation with a PHP page?

后端 未结 2 1462
既然无缘
既然无缘 2020-12-01 15:35

I\'m having hard time consuming a secure WCF Web Service from a PHP site. My knowledge in PHP are limited, I found various examples on the Web but didn\'t succeeded making t

相关标签:
2条回答
  • 2020-12-01 16:00

    SOAP's a bit funny with Authentication (or can be), I have used the method from http://www.php.net/manual/en/soapclient.soapclient.php#101503 see comment from, faebu at faebu dot ch 21-Dec-2010 02:58 for doing exactly this a few times... it may help

    Essentially uses CURL to pass the authentication credentials... it may/may not work for you!

    0 讨论(0)
  • 2020-12-01 16:04

    I solved the problem. I had to extends the "SoapHeader" class in PHP to make it compliant with the WS-Security standard.

    Here is the solution :

    PHP Header class

    class WsseAuthHeader extends SoapHeader 
    {
        private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
        function __construct($user, $pass, $ns = null) 
        {    
            if ($ns) 
            {        
                $this->wss_ns = $ns;    
            }    
    
            $auth = new stdClass();    
    
            $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);     
            $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);    
            $username_token = new stdClass();    
            $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);     
            $security_sv = new SoapVar(        
                                    new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),        
                                    SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);    
    
            parent::__construct($this->wss_ns, 'Security', $security_sv, true);
        }
    }
    

    PHP Client call

    $options = array( 
                'soap_version'    => SOAP_1_1, 
                'exceptions'      => true, 
                'trace'           => 1, 
                'wdsl_local_copy' => true
                );
    
    
    $username = "MyUser";
    $password = "MyPassword";
    
    $wsse_header = new WsseAuthHeader($username, $password);    
    
    $client = new SoapClient('https://UrlToService/Service.svc?wsdl', $options); 
    $client->__setSoapHeaders(array($wsse_header));
    
    try
    {
        $phpresponse = $client->Get(); 
    
        print $phpresponse->GetResult->Version;
        echo "</b><BR/><BR/>";
    }
    catch(Exception $e) 
    { 
        echo "<h2>Exception Error!</h2></b>"; 
        echo $e->getMessage(); 
    }
    

    Hope it will helps someone else!

    Thanks to Chris : Connecting to WS-Security protected Web Service with PHP

    0 讨论(0)
提交回复
热议问题