使用.NET客户端使用PHP Web服务时出现问题


Problem consuming PHP webservice with .NET client

我有一个关于用PHP编写的Web服务的问题。

为了使用PHP实例化SoapClient,我在PHP中的SoapClient方法上传递以下参数,如下所示:

$client = new SoapClient("some.wsdl", 
                         array('login' => "some_name",
                               'password' => "some_password"
                         ));

要用PHP调用Web服务上的方法,这很好。但是我的同事在使用.NET.发送凭据时遇到了问题

有人知道这是如何在.NET中传递的吗?

更新,网络客户端配置

<basicHttpBinding> 
   <binding name="webserviceControllerBinding"> 
       <security mode="TransportCredentialOnly"> 
           <transport clientCredentialType="Basic"/> 
       </security> 
    </binding> 
</basicHttpBinding> 

代码中:

client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";

更新2:

构建客户端(在PHP中(

$client = new 
SoapClient("http://***.***.***/********/httpdocs/webservice/wsdl?wsdl", 
    array(
        'uri'      => "http://***.***.***/***/httpdocs/webservice/webservice",
        'login'    => "*****",
        'password' => "*****"
));

处理请求并引用类的服务器。

public function webservice()
  parent::__construct();
  ini_set( 'soap.wsdl_cache_enabled', 0);
  $this->presenter     = "NONE";
  $server              = new SoapServer("http://***.***.***/***/httpdocs/webservice/wsdl?wsdl", array('uri' => "http://***.***.***/"));
  $server->setClass(   "Model_Webservice");
  $server->handle();

webservice类中的这一部分处理用户名和密码身份验证。

class Model_Webservice extends Object_Database
{
public function __construct()
{
  parent::__construct();
  $accesslog     = Log::factory('file', $this->config->log->access , 'ACCESS LOG');
  if(!isset($_SERVER['PHP_AUTH_USER']) || trim($_SERVER['PHP_AUTH_USER']) == "")
  {
     $accesslog->log("[denied] [soapclient " . $_SERVER['REMOTE_ADDR'] . "] no username supplied");
     throw new SOAPFault("Authentication failed - reason: no username supplied", 401);
  }
  elseif(!isset($_SERVER['PHP_AUTH_PW']) || trim($_SERVER['PHP_AUTH_PW']) == "")
  {
     $accesslog->log("[denied] [soapclient " . $_SERVER['REMOTE_ADDR'] . "] no password supplied");
     throw new SOAPFault("Authentication failed - reason: no password supplied", 401);
  }
  else
  {
     $user = new User(null, $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
     if($user->getId() > 0)
     {
        $accesslog->log("[approved] [soapclient " . $_SERVER['REMOTE_ADDR'] . "] User logged in");
     }
     if(!$user->getId() > 0)
     {
        $accesslog->log("[denied] [soapclient " . $_SERVER['REMOTE_ADDR'] . "] incorrect username and password combination");
        throw new SOAPFault("Authentication failed - reason: incorrect username and password combination", 401);
     }
  }
}

更新3

根据调整后的配置,我收到以下错误消息

  <bindings>
        <basicHttpBinding>
            <binding name="webserviceControllerBinding">
                <security mode="Message">                   
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>

BasicHttp绑定要求BasicHttpBinding.Security.Message.ClientCredentialType等效于安全消息的BasicHttpMessageCredentialType.Certificate凭据类型。为用户名凭据选择Transport或TransportWithMessageCredential安全性。'

我不想使用证书或https。

我建议使用像Fiddler这样的工具来捕获您的工作PHP客户端和非功能.NET客户端发送的实际SOAPXML数据。

如果你比较这两个请求,你很有可能会看到区别,并能够找出解决方法

希望能有所帮助。

我打赌.NET中的WCF不会发送身份验证标头(默认情况下(,除非收到来自Web服务的质询。

如果没有提供用户名或密码,则应返回HTTP状态代码401;如果用户名/密码不正确,则应返HTTP状态代码403。

换句话说:删除SoapException并返回正确的HTTP状态代码。

更新为对更新3的响应

嗯。这个异常与它是一个PHP Web服务这一事实无关。这是一个完全不同的问题。由于在没有证书的情况下使用BASIC身份验证会以CLEAR TEXT格式发送密码,因此会引发错误。

我会改用更安全的方式,比如DIGEST身份验证。

MessageHeader header= MessageHeader.CreateHeader("My-CustomHeader","http://myurl","Custom Header.");
       using (phptoebiTestclient.ServiceReference1.webserviceControllerPortTypeClient client = new phptoebiTestclient.ServiceReference1.webserviceControllerPortTypeClient())
       {
           using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
           {
               OperationContext.Current.OutgoingMessageHeaders.Add(header);
               HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
               byte[] toEncodeAsBytes    = System.Text.ASCIIEncoding.ASCII.GetBytes("username:password");                       
               httpRequestProperty.Headers.Add("Authorization: Basic "+ System.Convert.ToBase64String(toEncodeAsBytes));                                              
               OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
               string str = client.getVacanciesReference("");
           }                                 
       }

上面的代码已经解决了我的问题(我使用这些URL作为参考(

http://caught-in-a-web.blogspot.com/2008/04/how-to-add-custom-http-header-in-wcf.htmlhttp://en.wikipedia.org/wiki/Basic_access_authentication