4

2 つの数値を追加して値を返す自己ホスト型の wcf サービスがあります。正常に動作しますが、php クライアントを介してユーザー名とパスワードを送信する方法がわからないため、CustomUserNamePasswordValidator に対して検証します。Add メソッドの実装は次のとおりです。

public class MathService : IMathService
{
    public double Add(double x, double y)
    {
        return x + y;
    } 
}

ここに私の現在の App.Config があります:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
  <service behaviorConfiguration="MyServiceBehavior" name="WcfWithPhp.MathService">
    <endpoint address="" binding="basicHttpBinding" contract="WcfWithPhp.IMathService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8731/MathService" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

私は次のようにサービスを開始しています:

static void Main(string[] args)
{
    ServiceHost host = new ServiceHost(typeof(WcfWithPhp.MathService));
    host.Open();

    Console.WriteLine("Math Service Host");
    Console.WriteLine("Service Started!");

    foreach (Uri address in host.BaseAddresses)
    {
        Console.WriteLine("Listening on " + address);
    }

    Console.WriteLine("Press any key to close the host...");
    Console.ReadLine();
    host.Close();
}

PHPクライアントの場合、私はやっています:

<?php

header('Content-Type: text/plain');

echo "WCF Test\r\n\r\n";

// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient("http://localhost:8731/MathService?wsdl");

$obj->x = 2.5;
$obj->y = 3.5;

$retval = $client->Add($obj);

echo "2.5 + 3.5 = " . $retval->AddResult;

?>

上記は認証なしで正常に動作しますが、phpclient からユーザー名とパスワードを認証できるようにしたいと考えています。彼らが私のサービスにアクセスしようとするとき、現在次のように定義されている UserNamePasswordValidator のオーバーライドされた Validate メソッドを使用して、ユーザー名とパスワードを検証する必要があります。

public override void Validate(string userName, string password)
{
        if (string.IsNullOrEmpty(userName))
            throw new ArgumentNullException("userName");
        if (string.IsNullOrEmpty(password))
            throw new ArgumentNullException("password");

        // check if the user is not test
        if (userName != "test" || password != "test")
            throw new FaultException("Username and Password Failed");
 }

ユーザー名とパスワードの例として、 test と test を使用しています。動作構成の変更を設定し、バインディング構成を行う必要があることはわかっています。そのため、サービスは CustomUserNamePasswordValidator を使用しますが、PHP を知らないため、php から wcf サービスに資格情報を送信する方法がわかりません。認証情報が送信されると、wcf サービスで設定する方法がわかりません。私は wcf サービスを作成していません。client.ClientCredentials.UserName.UserNameと ができると思っていclient.ClientCredentials.UserName.Passwordましたが、これは私が作成していない .NET クライアントを作成している場合のみです。

私が持っていた別の質問は、クライアントが php クライアントの場合、basicHttpBinding だけに制限されているのでしょうか?

また、理想的には、php クライアントから wcf サービスに SOAP リクエストを送信することを望んでいるので、誰かが私を正しい方向に向けることができれば、それは素晴らしいことです。

私はちょうど次のことを試しましたが、うまくいきませんでした(Addが呼び出されましたが、認証されませんでした)

$sh_param = array('userName' => 'test', 'passWord' => 'test2');

$headers = new SoapHeader('http://localhost:8731/MathService.svc','UserCredentials',   
$sh_param,false);

$client->__setSoapHeaders(array($headers));

更新: 私の PHP Soap Client の初期化は次のとおりです。

$client = new SoapClient('http://localhost:8731/MathService?wsdl',
                         array('login' => "test2", 
                               'password' => "test",
                               'trace'=>1));

上記を実行することで、リクエストに次の内容が追加されました。

`Authorization: Basic dGVzdDI6dGVzdA==`

ただし、コンソール アプリでホストされている私の wcf サービスは、この承認を取得していません。ユーザー名の test とパスワードの test のハードコードされた値を持つカスタム ユーザー名バリデーターがありますが、「test2 "ログインのために、まだメソッドを呼び出しています。TransportWithCredentialOnly と Message="UserName" を使用しています

4

1 に答える 1

4

SoapClient コンストラクターのオーバーロードを試してください。

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

そして、ここにドキュメントがあります:http://www.php.net/manual/pl/soapclient.soapclient.php

于 2011-05-25T10:19:37.643 に答える