2017-06-06 35 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用戶代理報頭添加到所有請求消息的示例。我在我的一些客戶中使用了這個功能。

這是你腦子裏有什麼?