意外地将SOAP正文粘贴在SOAP Header中,试图使用WCF客户端访问WSE服务 [英] Accidentally sticking SOAP Body inside of SOAP Header attempting to access WSE service with WCF client

查看:152
本文介绍了意外地将SOAP正文粘贴在SOAP Header中,试图使用WCF客户端访问WSE服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

   <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主体粘贴到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;
                }
            }

我的PasswordDigentInspector

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));
        }

几乎所有东西都在那里.我不知道要在标题中注入主体的位置.任何机构都有想法吗?

更新:调试 我正在查看我插入到soap消息中的实际标题,这就是我看到的...

{<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中已经存在一个随机数和一个创建日期...不确定为什么它们没有出现在标题中...

<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包含这些东西呢? 看来我必须调试一下.

解决方案

如果不自己运行代码,则很难理解为什么我的示例在您的方案中不起作用.但是,为了尝试调试它,我将尝试以下操作:

在此行上设置一个断点:

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问题的人,首先打开详细的日志记录,然后使用提琴手来查看实际发生的事情.

I am attempting to hit a WSE secured web service using WCF. The SOAP header I need to generate to do this should contain a username, password, nonce, and created date... Here is a an example of a soap UI header that I use to hit the same service...

   <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>

Now I found a tutorial that was pretty usefull. usefull tutorial

I have successfully implemented it... But I am now sticking the SOAP body into the soap header and there isn't a nonce generated.

<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>

Don't know why it is doing this. I followed the tutorial to the letter. My Repository getting everything...

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;
                }
            }

My PasswordDigentInspector

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;
        }

How I apply client behavior

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

Everything is pretty much there. I am not seing where the body is being injected into the header. Any body have any ideas?

UPDATE: Debugging I am looking at the the actual header that I inject into the soap message and this is what I see...

{<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}

Just looking at that, there is no nonce, I could build it programmatically, but I am not sure if that is a good idea. Especially since there is a nonce already and a create date in the securityToken... Not sure why they don't appear in the header though...

<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>

I have the debugger stopped and I am watching both those variables. What might cause the header not to have those things and the the WSEHeader to have them? Looks like I will have to debugg that.

解决方案

Without running your code myself it is a little tricky to understand why my example doesn't work in your scenario. However, in a attempt to debug this I would try the following:

Stick a breakpoint on this line:

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

And after stepping through this line, see what the securityToken XML looks like. It is this XML which is being injected into the security header. It isn't any more complicated than that. You coudl create this XML manually if you want. I used the WSE 3.0 classes because I didn't want to hand code the XML myself.

I also notice that your security token definition looks slightly different to mine. My example was:

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

Yours:

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

The namespaces are different. Any reason for this?

If the breakpoint added from the step above isn't hit then it would suggest that the behavior isn't actually being applied. In which case, you'll probably need to double check your configs to make sure that the behavior is correctly applied on the client. Alternatively (as my example shows) you can programatically add the custom behavior before sending the message.

I recommend to anyone dealing with WCF issues, that they first turn on verbose logging and also use fiddler to see what is actually going over the wire.

这篇关于意外地将SOAP正文粘贴在SOAP Header中,试图使用WCF客户端访问WSE服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆