通过WSDL -C#获得结果



我正在尝试将此标头类添加到我的肥皂请求中,但看不到如何。作为实施的一部分,标题类是给我的,但是没有关于如何使用它的说明,我有点卡住了 - 我以前从未使用过WSDL和Web服务。我确定答案一定很容易,但我看不到。

标题要求

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

通常您需要添加行为扩展。

创建一个实现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;
}

然后创建一个将消息检查器应用于客户端运行时的端点行为。您可以使用属性或使用行为扩展元素来应用行为。

这是如何将HTTP用户代理标头添加到所有请求消息的示例。我在我的一些客户中使用过。

这是您想到的吗?

相关内容

  • 没有找到相关文章

最新更新