2013-02-22 91 views
2

錯誤[a:InvalidSecurity] An error occurred when verifying security for the message.InvalidSecurity當我嘗試SOAP調用我的WCF用戶名和密碼

我嘗試調用我的服務是這樣的。

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

我在這個問題上掙扎了好幾個星期,終於讓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> 

用這個來裝飾你的方法來控制w浩可以訪問哪些:

[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參數名稱完全匹配(區分大小寫)。

+0

謝謝對不起,如果我沒有提到它,然後我自己託管wcf,但我可以指定身份驗證嗎?我在我的soap調用中做了一些適應,並將安全性更改爲使用基本cridentialtype進行傳輸,然後得到此錯誤。 SoapFault [HTTP]在我的功能行中未經授權。 – Dendei 2013-02-25 08:56:07

+0

,但我仍然不能讓我的驗證器執行 – Dendei 2013-02-25 08:56:52

+1

現在它的工作現在與添加行並重命名我的PHP參數爲「登錄」和「密碼」不知道如何工作但可能沒有高大寫字母? – Dendei 2013-02-26 10:06:48

相關問題