PHP 클라이언트에서 자체 호스팅 WCF 서비스로 사용자 이름 / 암호 자격 증명을 어떻게 전달합니까?

StackOverflow https://stackoverflow.com/questions/6059524

문제

i 2 개의 숫자를 추가하고 값을 반환하는 자체 호스팅 WCF 서비스가 있습니다. 그것은 잘 작동하지만, PHP 클라이언트를 통해 사용자 이름과 암호를 보낼 수있는 방법을 모르으므로 CustomUsernAmepasswordValidator에 대한 유효성을 검사합니다. 다음은 추가 메서드의 구현입니다.

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의 재정의 유효성 검사 메소드를 통해 유효성을 검사하고 싶습니다.

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");
 }
.

나는 테스트를 사용하고 사용자 이름과 암호의 예로 테스트를 수행하고 있습니다. 나는 동작 구성을 수정하고 바인딩 구성을 수행해야하므로 서비스는 CustomUsernamepasswordValidator를 사용하지만 PHP를 모르는 이래로 PHP에서 WCF 서비스로 자격 증명을 보내는 방법을 모르겠습니다. 자격 증명이 전송되면 WCF 서비스에서 설정하는 방법을 모르겠습니다. WCF 서비스를 만들지 않습니다. 나는 client.ClientCredentials.UserName.UserNameclient.ClientCredentials.UserName.Password를 할 수 있었지만, 내가 아닌 .NET 클라이언트를 만드는 경우에만이 일을 할 수 있다고 생각했습니다.

다른 질문은 클라이언트가 PHP 클라이언트이면 BasichttpBinding 만 제한적으로 제한 되었습니까?

또한 이상적으로 내가하고 싶은 것은 PHP 클라이언트에서 WCF 서비스로 SOAP 요청을 보내므로 누구나이 방향으로 올바른 방향으로 가리킬 수 있다면 좋을 것입니다.

방금 다음을 시도했지만 작동하지 않았습니다 (추가가 호출되었지만 인증되지 않았습니다)

$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 = new SoapClient('http://localhost:8731/MathService?wsdl',
                         array('login' => "test2", 
                               'password' => "test",
                               'trace'=>1));
.

위를 수행하여 요청에 다음을 추가했습니다.

`Authorization: Basic dGVzdDI6dGVzdA==`
.

콘솔 응용 프로그램에서 호스팅되는 WCF 서비스는이 권한을 가져 오지 않고 암호에 대한 사용자 이름 및 테스트에 대한 테스트 값이 단단한 사용자 이름을 가진 사용자 정의 사용자 이름 유효성 검사기가 있지만 i 언제 로그인을 위해 "test2"를 시도해보십시오. 여전히 메서드를 호출합니다. TransportWithCredentialOnly 및 Message="사용자 이름"을 사용하고 있습니다.

도움이 되었습니까?

해결책

Try SoapClient constructor overload:

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

And here is the doc: http://www.php.net/manual/pl/soapclient.soapclient.php

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top