gpt4 book ai didi

.net - 如果我使用 WCF w/forms auth,php 或 java 客户端将如何进行身份验证?

转载 作者:行者123 更新时间:2023-12-01 09:11:38 25 4
gpt4 key购买 nike

我有一个通用的概念验证 WCF 服务,它使用表单例份验证来保护访问。当我的客户端是 .NET 时,一切都很好(下面的 vb 代码)

Dim client As SupplierServiceClient = New SupplierServiceClient()
client.ClientCredentials.UserName.UserName = "xxxx@xxx.xx.xx"
client.ClientCredentials.UserName.Password = "password"

Dim SupplierList As List(Of Supplier) = client.GetSuppliers()

但由于我希望它与任何可以执行 SOAP 1.1/1.2 的人互操作 - PHP 或 Java 客户端如何连接?

下面列出了我的 WCF web.config (fyi)

<system.serviceModel>
<services>
<service name="SampleApplicationWCF.Library.SupplierService" behaviorConfiguration="NorthwindBehavior">
<endpoint address="" name="wsHttpSupplierService" contract="SampleApplicationWCF.Library.ISupplierService" binding="wsHttpBinding" bindingConfiguration="wsHttp"/>
<endpoint address="https://server/SampleApplicationWCF/SupplierService.svc/Basic" name="basicHttpSupplierService" contract="SampleApplicationWCF.Library.ISupplierService" binding="basicHttpBinding" bindingConfiguration="basicHttp"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="wsHttp">
<security mode="TransportWithMessageCredential">
<transport/>
<message clientCredentialType="UserName" negotiateServiceCredential="false" establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
<basicHttpBinding>
<binding name="basicHttp">
<security mode="TransportWithMessageCredential">
<transport/>
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="NorthwindBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceAuthorization principalPermissionMode="UseAspNetRoles"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

最佳答案

我设法验证了一个 PHP 客户端,但并非不费吹灰之力。起初,我尝试了以下方法:

$header = new stdClass;
$credentials = new stdClass;
$credentials->Username="myuser";
$credentials->Password="mypass";
$header->UsernameToken = $credentials;

$securityNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
$client->__setSoapHeaders($securityNamespace, 'Security', $header);

没有用。似乎 PHP5(在我的例子中是 v5.3.5)有一个错误,阻止命名空间前缀出现在嵌套的标题标签内。 http://bugs.php.net/bug.php?id=48966
我最终得到:

<UsernameToken><Username>myuser</Username><Password>myuser</Password></UsernameToken>

代替:

<o:UsernameToken><o:Username>myuser</o:Username><o:Password>myuser</o:Password></o:UsernameToken>

所以,我要做的就是将必要的 XML 硬编码到一个变量中。这很丑陋但有效:

$securityNamespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
$headerContent = "<o:Security xmlns:o=\"$securityNamespace\">
<o:UsernameToken>
<o:Username>myuser</o:Username>
<o:Password>mypass</o:Password>
</o:UsernameToken>
</o:Security>";
$headerVar = new SoapVar($headerContent, XSD_ANYXML, null, null, null);
$header = new SoapHeader($securityNamespace, 'o:Security', $headerVar);
$client->__setSoapHeaders(array($header));

我希望它对某人有所帮助。再见!

关于.net - 如果我使用 WCF w/forms auth,php 或 java 客户端将如何进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/513330/

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