2017-06-06 71 views
0

我想这个头类添加到我的SOAP请求,但不知道怎样。页眉类是给我作为执行工作的一部分,但有关于如何使用它没有说明,我有点坚持 - 我以前没使用WSDL和Web服务。我相信答案一定是非常容易的,但我看不到它。通过WSDL获得结果 - C#

部首要求

<soapenv:Header> 
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
    <wsse:UsernameToken wsu:Id="UsernameToken-19" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
     <wsse:Username>##USERNAME##</wsse:Username> 
     <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">##PASSWORD##</wsse:Password> 
    </wsse:UsernameToken> 
    </wsse:Security> 
</soapenv:Header> 

部首类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel.Channels; 
using System.Web; 
using System.Xml; 
using System.Xml.Serialization; 

namespace Consuming 
{ 
    public class SecurityHeader : MessageHeader 
    { 
     private readonly UsernameToken _usernameToken; 

     public SecurityHeader(string id, string username, string password) 
     { 
      _usernameToken = new UsernameToken(id, username, password); 
     } 

     public override string Name 
     { 
      get { return "Security"; } 
     } 

     public override string Namespace 
     { 
      get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; } 
     } 

     protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(UsernameToken)); 
      serializer.Serialize(writer, _usernameToken); 
     } 
    } 


    [XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] 
    public class UsernameToken 
    { 
     public UsernameToken() 
     { 
     } 

     public UsernameToken(string id, string username, string password) 
     { 
      Id = id; 
      Username = username; 
      Password = new Password() { Value = password }; 
     } 

     [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")] 
     public string Id { get; set; } 

     [XmlElement] 
     public string Username { get; set; } 

     [XmlElement] 
     public Password Password { get; set; } 
    } 

    public class Password 
    { 
     public Password() 
     { 
      Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; 
     } 

     [XmlAttribute] 
     public string Type { get; set; } 

     [XmlText] 
     public string Value { get; set; } 
    } 
} 

代码

var mySoapHeader = new SecurityHeader("ID","Username","password"); 
var client = new GroupWebServiceClient(); // Created from Add Web Reference 

client.?????? = mySoapHeader;// I can't see how to add the Header to the request 

var response = new groupNameListV1(); 
response = client.getAllDescendants("6335");//This needs the header - omitting gives "An error was discovered processing the <wsse:Security> header" 

编辑

我想通了,最终,事实证明这是很容易 - 增加的情况下,任何解决方案其他发现它有用

using (new OperationContextScope(client.InnerChannel)) 
    { 
     OperationContext.Current.OutgoingMessageHeaders.Add(
        new SecurityHeader("ID", "USER", "PWD")); 

     var response = new groupNameListV1(); 
     response = client.getAllDescendants("cng_so_6553"); 
     //other code 
    } 
+1

https://stackoverflow.com/questions/5833539/how-to-add-security-header-to-a-soap-message – shop350

回答

0

一般需要添加行为扩展。

创建一个实现IClientMessageInspector类。在BeforeSendRequest方法中,将自定义标题添加到传出消息。它可能是这个样子:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) 
{ 
HttpRequestMessageProperty httpRequestMessage; 
object httpRequestMessageObject; 
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject)) 
{ 
    httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty; 
    if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER])) 
    { 
     httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent; 
    } 
} 
else 
{ 
    httpRequestMessage = new HttpRequestMessageProperty(); 
    httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent); 
    request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage); 
} 
return null; 
} 

然后创建应用于消息检查到客户端运行时的终结点行为。您可以通过属性或通过使用行为扩展元素的配置来应用行为。

Here是如何的HTTP用户代理报头添加到所有请求消息的示例。我在我的一些客户中使用了这个功能。

这是你脑子里有什么?