gpt4 book ai didi

php - 如何使用对 PHP 页面使用自定义用户名验证的 WCF Web 服务?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:44 27 4
gpt4 key购买 nike

我很难从 PHP 站点使用安全的 WCF Web 服务。我对 PHP 的了解有限,我在网上找到了各种示例,但还没有成功使它们工作。

我有一个 Silverlight 应用程序也使用此 WebService,它工作正常。但是,当我运行 PHP 站点时,出现此错误:

MessageSecurityException: Security processor was unable to find a security header in the message. This might be because the message is an unsecured fault or because there is a binding mismatch between the communicating parties. This can occur if the service is configured for security and the client is not using security.

我还尝试将 customBinding 更改为 basicHttpBinding。当我这样做时,不再调用自定义用户名验证器。但是,在我的 WCF 服务上使用 basicHttpBinding 并删除凭据验证“有效”(除了它是不安全的事实)。所以问题似乎与安全消息有关。

有人可以给我一个可以帮助我使这个 PHP 页面正常工作的工作示例或教程吗?

这是我的 PHP 代码:

    $options = array( 
'soap_version' => SOAP_1_1,
'exceptions' => true,
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'Username' => 'MyUserName',
'password' => 'MyPassword');

$client = new SoapClient('https://UrlToService/Service.svc?wsdl', $options);

try
{
$phpresponse = $client->Get();

print $phpresponse->GetResult->Version;
echo "</b><BR/><BR/>";
}
catch(Exception $e)
{
echo "<h2>Exception Error!</h2></b>";
echo $e->getMessage();
echo "<BR/><BR/>";
}

WCF 配置

<behavior name="sslBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyNamespace.ServiceUserNameValidator, MyNamespace" />
</serviceCredentials>
<serviceSecurityAudit
auditLogLocation="Application"
serviceAuthorizationAuditLevel="Failure"
messageAuthenticationAuditLevel="Failure"
suppressAuditFailure="true" />
</behavior>

<customBinding>
<binding name="sslCustomBinding">
<security authenticationMode="UserNameOverTransport" includeTimestamp="true" allowInsecureTransport="true">
<localServiceSettings maxClockSkew="00:10:00" />
<localClientSettings maxClockSkew="00:10:00" />
<secureConversationBootstrap />
</security>
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport />
</binding>
</customBinding>

<service behaviorConfiguration="sslBehavior" name="MyNamespace.Services.Service">
<endpoint address="" binding="customBinding"
bindingConfiguration="sslCustomBinding" contract="MyNamespace.ServiceContracts.IService" />
<host>
<baseAddresses>
<add baseAddress="https://UrlToService/Services/" />
</baseAddresses>
</host>
</service>

最佳答案

我解决了这个问题。我必须扩展 PHP 中的“SoapHeader”类,使其符合 WS-Security 标准。

解决方法如下:

PHP header 类

class WsseAuthHeader extends SoapHeader 
{
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
function __construct($user, $pass, $ns = null)
{
if ($ns)
{
$this->wss_ns = $ns;
}

$auth = new stdClass();

$auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$username_token = new stdClass();
$username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);
$security_sv = new SoapVar(
new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);

parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}

PHP 客户端调用

$options = array( 
'soap_version' => SOAP_1_1,
'exceptions' => true,
'trace' => 1,
'wdsl_local_copy' => true
);


$username = "MyUser";
$password = "MyPassword";

$wsse_header = new WsseAuthHeader($username, $password);

$client = new SoapClient('https://UrlToService/Service.svc?wsdl', $options);
$client->__setSoapHeaders(array($wsse_header));

try
{
$phpresponse = $client->Get();

print $phpresponse->GetResult->Version;
echo "</b><BR/><BR/>";
}
catch(Exception $e)
{
echo "<h2>Exception Error!</h2></b>";
echo $e->getMessage();
}

希望它能帮助到其他人!

感谢克里斯:Connecting to WS-Security protected Web Service with PHP

关于php - 如何使用对 PHP 页面使用自定义用户名验证的 WCF Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6652227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com