4

エラー[a:InvalidSecurity] An error occurred when verifying security for the message.

私はこのように自分のサービスを呼び出そうとします。

php

<?php
    $soapURL = "https://localhost:8888/wcf/?wsdl" ;
    $soapParameters = Array('userName' => "user23", 'password' => "pass123") ;

    $soapClient = new SoapClient($soapURL, $soapParameters);

    var_dump($soapClient->GetVersion());
?>

以前は認証なしで動作するようになったので、問題はここのphpコードまたはWCFのapp.configにあります。

ここに私のapp.configがあります

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service
        behaviorConfiguration="MYDLL.Behavior"
        name="MYDLL.WCFService">
        <endpoint
          address=""
          binding="basicHttpBinding"
          bindingConfiguration="UsernameAndSSL"
          name="basicHttpBinding"
          contract="MYDLL.IService"/>
        <endpoint
          address="mex"
          binding="mexHttpsBinding"
          bindingConfiguration=""
          name="MexBinding"
          contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8734/wcf/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MYDLL.Behavior">
          <serviceMetadata httpsGetEnabled="true"/>
            <serviceCredentials>
              <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MYDLL.CustomUserNameValidator, MYDLL"/>
              <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
            </serviceCredentials>
          <useRequestHeadersForMetadataAddress />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="UsernameAndSSL">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Basic"/>
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>
</configuration>

およびcustomusernamevalidator

public class CustomUserNameValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        Console.WriteLine("User validation succeeded (Username: {0}; Password: {1})", userName, password);
    }
}

私が得られないのは、障害が私のphpコードまたは構成にある場合です。consolewriteが実行されず、invalidSecurity 例外が発生するだけなので、見つけられない customusernamevalidation があることがわかっているようです。

4

1 に答える 1

4

私はこの同じ問題に数週間苦労し、ついにPHPクライアントに私のWCFサービスで認証を依頼しました。この構成は、私が現在本番環境で作業しているものです。

まず、匿名認証と基本認証を有効にする必要があります。クライアントが認証する前にWSDLを読み取れるように、匿名をオンのままにしておく必要があります。匿名認証が有効になっている場合でも、最初に認証されない限り、サービスを使用することはできません。

これをweb.configに入れるか、IISで匿名および基本認証を直接オンにします。

<security>
   <authentication>
      <anonymousAuthentication enabled="true" />
      <basicAuthentication enabled="true" />
   </authentication>
</security> 

次に、基本認証とトランスポートセキュリティを使用するようにWCFセキュリティを設定します。

<bindings>
  <basicHttpBinding>
    <binding name="SSLBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>        
  </basicHttpBinding>
</bindings>

そして、これが私の行動です:

<behaviors>
  <serviceBehaviors>
    <behavior name="SSLBehavior">
      <serviceMetadata httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceSecurityAudit auditLogLocation="Application"
                            serviceAuthorizationAuditLevel="Failure"
                            messageAuthenticationAuditLevel="Failure"
                            suppressAuditFailure="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

これを使用して、メソッドを装飾し、誰が何にアクセスできるかを制御します。

[PrincipalPermission(SecurityAction.Demand, Role = @"DOMAIN\ActiveDirectoryGroup")]

この設定を使用すると、PHPクライアントはWSDLを取得し、承認後にメソッドを呼び出すことができるはずです。

$url = 'https://domain.com/service.svc?wsdl';

$username = 'username';
$password = 'password';

$options = array(
    'login' => $username,
    'password' => $password,
    'soap_version' => SOAP_1_1,
    'exceptions' => true,
    'trace' => 1,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
    'connection_timeout' => 60
);

$client = new SoapClient($url, $options);

$obj = new YourObject();

$obj->MyProperty = 'Test';
$obj->MyOtherProperty = 'Testing';

$result = $client->WCFMethodName(array('paramName' => $obj)); 

PHPコードのパラメーター名がWCFパラメーター名と完全に一致することが非常に重要です(大文字と小文字が区別されます)。

于 2013-02-23T11:31:25.367 に答える