【问题标题】:How would a php or java client authenticate if I'm using WCF w/ forms auth?如果我使用带表单身份验证的 WCF,php 或 java 客户端将如何进行身份验证?
【发布时间】:2010-10-05 12:39:51
【问题描述】:

我有一个通用的概念验证 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>

【问题讨论】:

    标签: .net wcf security interop


    【解决方案1】:

    我设法验证了一个 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));
    

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

    【讨论】:

      【解决方案2】:

      免责声明:我不是 Java 或 PHP 开发人员

      通过在客户端对象上设置 UserName 值,您就是在告诉 WCF 将该信息作为 SOAP 标头的一部分通过网络传送。由于 SOAP 是一种被广泛采用的标准,我确信有一些库可以帮助您将这些信息添加到 SOAP 消息头中。我会寻找可以帮助您使用 WS-* 和/或 WS-I Basic Profile 规范来使用服务的东西。

      祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-13
        • 2011-04-09
        • 2013-10-07
        • 2018-02-12
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        相关资源
        最近更新 更多