NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Soap Server with .net type Soap Header Authentication


Joined: 06 Apr 2008
Posts: 2
Location: Irvine, California
Reply with quote
Hi,

I was wondering if anyone knows how to include .net type soap headers (typically used for authentication) on the SERVER side of nusoap.

So assuming the following sample server code from an IBM redbook...

Code:
<?
require_once("./lib/nusoaplib/nusoap.php");
$ns="http://myurl";

$server = new soap_server();
$server->configureWSDL('ITSOcalcTax',$ns);
$server->wsdl->schemaTargetNamespace=$ns;

$server->register('taxcalc',
array ('amount' => 'xsd:string'),
array ('return' => 'xsd:string'),
$ns);

function taxcalc($amount) {
$calc=$amount * .08;
return new soapval('return','string',$calc);
}

$server->service($HTTP_RAW_POST_DATA);
?>


how do I include soap headers such as...


Code:
$header = (
"<UserCredentials xmlns='http://myurl/'>".
"<userId>".htmlspecialchars($user)."</userId>".
"<password>".htmlspecialchars($pass)."</password>".
"</UserCredentials>"
);


so that I can call the service with the following code...

Code:
<?
require_once("./lib/nusoaplib/nusoap.php");
$wsdl="http://myurl?wsdl";
$client=new soapclient($wsdl,'wsdl');
$user='Guest';
$pass='Test1234';
$param=array('amount'=>1000);

$header = (
"<UserCredentials xmlns='http://myurl/'>".
"<userId>".htmlspecialchars($user)."</userId>".
"<password>".htmlspecialchars($pass)."</password>".
"</UserCredentials>"
);
$results=$client->call('ITsocalcTax',$param,false,false,$header);
print_r($results);
?>


The "UserCredentials" class is pre-defined in the .net world and I need to conform to this.

Thank you.

-a
View user's profileFind all posts by bit_witSend private message
Reply from Scott at sourceforge


Joined: 06 Apr 2008
Posts: 2
Location: Irvine, California
Reply with quote
Hi,

I am posting the reply below that I got from Scott Nichol at sourceforge for the benefit of others asking the same question. I hope that's ok.

Please add more to this thread if you have any input.

Thank you.

-a


RE: nuSoap Server with .net type Soap Header (New)
By: Scott Nichol (snichol) - 2008-04-08 09:00
There are a couple of instance variables in the nusoap_server class that allow you to access SOAP Headers. Most generally, requestSOAP gives you the full SOAP request (the HTTP payload) as a string. You can then parse anything out of it that you want. The requestHeaders variable gives you just the SOAP headers in string form, with a caveat: namespace resolution is spotty or nonexistent. Finally, requestHeader gives you the headers parsed into associative arrays, similar to what you get as a return value from the call method of nusoap_client.

Your code might look like this (with no error checking):

Code:
function taxcalc($amount) { 
  global $server;
 
  $userId = $server->requestHeader['UserCredentials']['userId'];
  $password = $server->requestHeader['UserCredentials']['password'];
 
  if (authenticate($userId, $password)) {
    $calc=$amount * .08; 
    return new soapval('return','string',$calc); 
  } else {
    return new nusoap_fault('SOAP-ENV:Client', '', 'Authentication failed');
  }



RE: nuSoap Server with .net type Soap Header (New)
By: bit wit (bit_wit) - 2008-04-09 01:18
Hi Scott,

I am not sure yet what the issues will be about the namespace, but I will try what you suggested.

Thank you for answering me.

-a
View user's profileFind all posts by bit_witSend private message
Reply from Scott at sourceforge


Joined: 25 Apr 2008
Posts: 1
Reply with quote
Hi, thanks for details.

I tried to transfer SOAP header from .NET client to PHP web service as this method was described in this post. But instead login and password values on server side I have that $server->requestHeader is invalid expression in watch window. Sad Why? Thanks for any help!
PHP version is 5.2.5, Apache version is 2.2.

PHP web service is the same.
.NET client:

Code:
try
            {
                ITSOcalcTax service = new ITSOcalcTax();
                service.Credentials = new NetworkCredential("a", "b");
                service.Url = "http://localhost/PhpWebServices/myservice.php?XDEBUG_SESSION_START=3";

                string tax = service.taxcalc("100");

                Console.WriteLine("tax is " + tax);
            }
            catch (Exception exception)
            {
                Console.WriteLine("exception is " + exception.ToString());
            }
            Console.Read();
View user's profileFind all posts by edmasterSend private message
.NET NuSoap Header Athentication problem


Joined: 26 Aug 2009
Posts: 1
Reply with quote
Hi everybody

I have found a way to do a personal authentication on a web service. In the soap header message, you can set nodes with that information. So, each time the client consume a web service, he/she must athuenticate him/her self. Now, the problem is that don't work forme. T-T

Check the exaple at the bottom, http://msdn.microsoft.com/es-es/library/8728chd5.aspx (in spanish)

Now, I did a web service that brings the user name seted inside the header, the problem is that is not founded.


This. is my web service:


MyWebService.asmx


Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' Define a SOAP header by deriving from the SoapHeader base class.
Public Class Header : Inherits SoapHeader
Public Username As String
Public Password As String
End Class


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri_org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class MyWebService
' Add a member variable of the type deriving from SoapHeader.
Public myHeaderMemberVariable As Header

' Apply a SoapHeader attribute.
<WebMethod(), SoapHeader("myHeaderMemberVariable")> _
Public Function MyWebMethod() As String
' Process the SoapHeader.
Return myHeaderMemberVariable.Username
End Function
End Class


My test file:

test1.php


<?php
require_once('nusoap_0-7-3/lib/nusoap.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Probando los Web Services desde PHP</title>
</head>
<body>
<?php
$wsdl = 'http://localhost/ws/MyWebService.asmx?WSDL';
$method = 'MyWebMethod';
$params = array();
$client = new nusoap_client($wsdl, 'wsdl');
//$client->response_timeout = 600;

$client->soap_defencoding = 'UTF-8';
$client -> setHeaders('
<Header>
<Username>myUserName</Username>
<Password>myPassword</Password>
</Header>
');
$err = $client->getError();
//echo "<br/> proxyport =".htmlspecialchars($client->$proxyhost)."<br/>";

if ($err) {
echo '<h2>Error del contructor</h2><pre>' . $err . '</pre>';
echo '<h2>Debug:</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
$result = $client->call($method, $params);

if ($client->fault) {
echo '<h2>Fault</h2><pre>'; print_r($result); echo '</pre>';
} else {
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>';
}
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
?>
</body>
</html>


My Issue 0_o

Fault

Array
(
[faultcode] => soap:Server
[faultstring] => System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.

at meals_ws_application.MyWebService.MyWebMethod() in C:\Documents and Settings\ingenieria\Mis documentos\Visual Studio 10\Projects\ws\meals-ws-application\MyWebService.asmx.vb:line 25
--- End of inner exception stack trace ---

[detail] =>
)

Request

POST /ws/MyWebService.asmx HTTP/1.0

Host: localhost
User-Agent: NuSOAP/0.7.3 (1.114)
Content-Type: text/xml; charset=UTF-8
SOAPAction: "http://tempuri_org/MyWebMethod"

Content-Length: 554

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns8315="http://tempuri_org"><SOAP-ENV:Header>

<Header>
<Username>myUserName</Username>
<Password>myPassword</Password>
</Header>
</SOAP-ENV:Header><SOAP-ENV:Body><MyWebMethod xmlns="http://tempuri_org/"></MyWebMethod></SOAP-ENV:Body></SOAP-ENV:Envelope>



I just want that the web service return myUserName, that's it T-T
If it works I could set some encripted information. All your posts are wellcome.




Regards


Jorge Andrés Cañón Sierra
Ingenierio de Sistemas
View user's profileFind all posts by jacanon7Send private message


Joined: 02 Dec 2010
Posts: 1
Reply with quote
I don't know if you will receive this message because this forum has been inactive for some time but I had problems too. What web server would you recommend that I use to transfer the SOAP header from .NET??

Thanks,
View user's profileFind all posts by kierstenSend private messageMSN Messenger


Joined: 20 Dec 2010
Posts: 1
Reply with quote
Hi! I’ve just visited this forum. Happy to get acquainted with you. Thanks.

__________________
View user's profileFind all posts by rachely476Send private message
Soap Server with .net type Soap Header Authentication
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic