0

我正在尝试使用 WCF 访问 WSE 安全 Web 服务。为此,我需要生成的 SOAP 标头应包含用户名、密码、随机数和创建日期……这是我用来访问相同服务的一个肥皂 UI 标头示例……

   <soap:Header>
      <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>----------</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">----------</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">Hozef94FFwOhuiF5QixaMQ==</wsse:Nonce>
            <wsu:Created>2012-08-21T13:26:03.642Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soap:Header>

现在我找到了一个非常有用的教程。 有用的教程

我已经成功地实现了它......但是我现在将 SOAP 主体粘贴到肥皂头中,并且没有生成随机数。

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Header>
        <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo9VZylDHg5JMgjsNnWLhATkAAAAA+YtOxHdh0Uqd4a64raX/nIzYz20mPHlBv4Wk5S8d5PsACQAA</VsDebuggerCausalityData>
        <wsse:Security s:mustUnderstand="0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
            <UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                <Username>------------</Username>
                <Password>************</Password>
            </UsernameToken>
        </wsse:Security>
        <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <GetOrganizations xmlns="http://------------------------/businessobjects/messaging/">
                <personId xmlns="">0</personId>
                <typeId xmlns="">
                    <int>1</int>
                    <int>2</int>
                    <int>3</int>
                    <int>4</int>
                </typeId>
            </GetOrganizations>
        </s:Body>
    </s:Header>
</s:Envelope>

不知道它为什么这样做。我完全按照教程进行操作。我的存储库得到了一切......

using (DistListServiceReference.DistributionListClient dlc = new DistListServiceReference.DistributionListClient())
            {
                try
                {
                    PasswordDigestBehavior behavior = new PasswordDigestBehavior("********", "********");                            
                    dlc.Endpoint.Behaviors.Add(behavior);

                    GetDistributionLists gdl = new GetDistributionLists();

                    gdl.PersonID = 0;

                    GetDistributionListsResponse gdlr = new GetDistributionListsResponse();

                    gdlr = dlc.GetDistributionLists(gdl);
                    return gdlr;
                }
                catch (Exception e)
                {
                    dlc.Abort();
                    return null;
                }
            }

我的密码DigentInspector

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            // Use the WSE 3.0 security token class
            UsernameToken token = new UsernameToken(this.Username, this.Password, PasswordOption.SendPlainText);
            WseHeader header = new WseHeader(this.Username, this.Password);

            // Serialize the token to XML
            XmlElement securityToken = token.GetXml(new XmlDocument());           

            MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken", securityToken, false);
            request.Headers.Add(header);

            // complete
            return Convert.DBNull;
        }

我如何应用客户行为

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new PasswordDigestMessageInspector(this.Username, this.Password));
        }

一切都在那里。我没有看到身体被注入头部的位置。任何机构有任何想法?

更新:调试 我正在查看注入到肥皂消息中的实际标头,这就是我所看到的...

{<wsse:Security s:mustUnderstand="0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <Username>**********</Username>
    <Password>************</Password>
  </UsernameToken>} System.ServiceModel.Channels.MessageHeaderInfo {TestDistListApplication.Repository.WseHeader}

只是看看,没有随机数,我可以以编程方式构建它,但我不确定这是否是个好主意。特别是因为在securityToken中已经有一个nonce和一个创建日期......不知道为什么它们没有出现在标题中......

<wsse:Username xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">MedTrak_Dev</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">uncJUN132012</wsse:Password>
<wsse:Nonce xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">mvy9nUfF+rnT3oTasDBqxg==</wsse:Nonce>
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2012-08-28T13:30:42Z</wsu:Created>

我停止了调试器,我正在观察这两个变量。什么可能导致标头没有这些东西而 WSEHeader 有它们?看起来我将不得不调试它。

4

1 回答 1

1

如果不自己运行您的代码,理解为什么我的示例在您的场景中不起作用有点棘手。但是,为了调试它,我会尝试以下操作:

在这一行设置断点:

XmlElement securityToken = token.GetXml(new XmlDocument());

在单步执行此行之后,查看 securityToken XML 的样子。正是这个 XML 被注入到安全头中。没有比这更复杂的了。如果需要,您可以手动创建此 XML。我使用了 WSE 3.0 类,因为我不想自己手动编写 XML 代码。

我还注意到您的安全令牌定义与我的略有不同。我的例子是:

MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityToken, false);

你的:

MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken", securityToken, false);

命名空间不同。这有什么原因吗?

如果未命中从上述步骤添加的断点,则表明该行为实际上并未被应用。在这种情况下,您可能需要仔细检查您的配置以确保该行为正确应用于客户端。或者(如我的示例所示)您可以在发送消息之前以编程方式添加自定义行为。

我建议任何处理 WCF 问题的人,他们首先打开详细日志记录并使用 fiddler 来查看实际发生的情况。

于 2012-08-28T13:01:23.480 回答